我正在开发一个应用程序,我从system中获取Installed app并成功显示在listview中。我成功地完成了每件事。但现在在这里,我也得到了我不想要的系统应用程序。 系统应用程序像...浏览器,设置,这样的闹钟,所以我不需要。那我该怎么做才能过滤这个? 你可以帮我解决这个问题吗?
代码::
private ArrayList<PInfo> getInstalledApps(boolean getSysPackages) {
ArrayList<PInfo> res = new ArrayList<PInfo>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
// Fill PackageInfo
if ((!getSysPackages) && (p.versionName == null)) {
continue;
}
PInfo newInfo = new PInfo();
newInfo.appname = p.applicationInfo.loadLabel(getPackageManager())
.toString();
newInfo.pname = p.packageName;
newInfo.versionName = p.versionName;
newInfo.versionCode = p.versionCode;
PackageManager pm = getPackageManager();
ApplicationInfo appInfo;
try {
// Get all app information in Application info
appInfo = pm.getApplicationInfo(newInfo.pname, 0);
String appFile = appInfo.sourceDir;
newInfo.size = String.valueOf(new File(appFile).length());
long installed = new File(appFile).lastModified();
newInfo.date = String.valueOf(installed);
Date dtappdate = new Date(installed);
newInfo.instdate = String.valueOf(dtappdate.toString());
} catch (NameNotFoundException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
res.add(newInfo);
}
return res;
}
答案 0 :(得分:1)
试试这个:
PackageManager packageManager = getPackageManager();
List<PackageInfo> packageList = packageManager.getInstalledPackages(0);
for (PackageInfo pi : packageList) {
ApplicationInfo ai = pi.applicationInfo;
if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) == 1){
Log.d("APP_NAME", ai.loadLabel(packageManager).toString());
Log.d("PACKAGE_NAME", pi.packageName);
Log.d("APP_VERSION", pi.versionName);
}
}