Android中获取应用信息

获取应用信息

工作中需要经常获取App的各种信息:现在写一个封装类

这里主要看Code

public class AppInfo implements Parcelable {

private String appLabel;
private Drawable appIcon;
private Intent intent;
private String pkgName;
private Date date;
private int number;

private long cachesize;
private long datasize;
private long codesieze;
static final int MAX_CHARACTER_COUNT = 9;

protected AppInfo(Parcel in) {
    appLabel = in.readString();
    intent = in.readParcelable(Intent.class.getClassLoader());
    pkgName = in.readString();
    number = in.readInt();
    cachesize = in.readLong();
    datasize = in.readLong();
    codesieze = in.readLong();
}

public static final Creator<AppInfo> CREATOR = new Creator<AppInfo>() {
    @Override
    public AppInfo createFromParcel(Parcel in) {
        AppInfo appInfo = new AppInfo();
        appInfo.setAppLabel(in.readString());
        appInfo.setIntent((Intent) in.readParcelable(Intent.class.getClassLoader()));
        appInfo.setPkgName(in.readString());
        appInfo.setNumber(in.readInt());
        appInfo.setCachesize(in.readLong());
        appInfo.setDatasize(in.readLong());
        appInfo.setCodesieze(in.readLong());
        return new AppInfo(in);
    }

    @Override
    public AppInfo[] newArray(int size) {
        return new AppInfo[size];
    }
};

public long getCachesize() {
    return cachesize;
}

public void setCachesize(long cachesize) {
    this.cachesize = cachesize;
}

public long getDatasize() {
    return datasize;
}

public void setDatasize(long datasize) {
    this.datasize = datasize;
}

public long getCodesieze() {
    return codesieze;
}

public void setCodesieze(long codesieze) {
    this.codesieze = codesieze;
}

public AppInfo() {
}

public String getAppLabel() {
    return appLabel;
}

public void setAppLabel(String appName) {
    this.appLabel = appName;
}

public Drawable getAppIcon() {
    return appIcon;
}

public void setAppIcon(Drawable appIcon) {
    this.appIcon = appIcon;
}

public Intent getIntent() {
    return intent;
}

public void setIntent(Intent intent) {
    this.intent = intent;
}

public String getPkgName() {
    return pkgName;
}

public void setPkgName(String pkgName) {
    this.pkgName = pkgName;
}

public Date getDate() {
    return date;
}

public void setDate(Date date) {
    this.date = date;
}

public void setNumber(int number) {
    this.number = number;
}

public int getNumber() {
    return number;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeString(appLabel);
    parcel.writeParcelable(intent, i);
    parcel.writeString(pkgName);
    parcel.writeInt(number);
    parcel.writeLong(cachesize);
    parcel.writeLong(datasize);
    parcel.writeLong(codesieze);
}

public String limitNameLength(String appName, Context mContext, AppInfo appInfo) {
    int mCharacterCount = 0;
    int mInputNum = 0;
    for (int i = 0; i<appName.length(); i++) {
         String str = appName.substring(i, i + 1);
         mInputNum++;
         mCharacterCount += regexInput(str);
         if (mCharacterCount > MAX_CHARACTER_COUNT) {
            break;
         }
    }
    if (mCharacterCount > MAX_CHARACTER_COUNT && (mCharacterCount % 2 == 0)) {
        appName = appName.substring(0,mInputNum);
        if (!appName.equals(appInfo.getAppLabel())) {
            appName += mContext.getString(R.string.omit);
        }

    } else if (mCharacterCount > MAX_CHARACTER_COUNT && (mCharacterCount % 2 == 1)) {
        appName = appName.substring(0, mInputNum - 1) + mContext.getString(R.string.omit);
    }
    return appName;
}

private int regexInput(String subString) {
    Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
    Matcher m = p.matcher(subString);
    if (m.matches()) {
        return 2;
    } else {
        return 1;
    }
}

}



看应用这个类

PackageManager pm = this.getPackageManager();
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> resolveInfos = pm.queryIntentActivities(mainIntent, 0);

for (ResolveInfo reInfo : resolveInfos) {
            File file = new File(reInfo.activityInfo.applicationInfo.sourceDir);
            Date systemDate = new Date(file.lastModified());
            ApplicationInfo applicationInfo = reInfo.activityInfo.applicationInfo;
            String activityName = reInfo.activityInfo.name;
            String pkgName = reInfo.activityInfo.packageName;
            String appLabel = (String) reInfo.loadLabel(pm);
            Drawable icon = reInfo.loadIcon(pm);
            Intent launchIntent = new Intent();
            launchIntent.setComponent(new ComponentName(pkgName, activityName));
            AppInfo appInfo = new AppInfo();
            appInfo.setAppLabel(appLabel);
            appInfo.setPkgName(pkgName);
            appInfo.setDate(systemDate);
            appInfo.setAppIcon(icon);
            appInfo.setIntent(launchIntent);
            mlistAppInfo.add(appInfo);
        }

三段代码搞定(没有注释直接copy使用)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值