android 推断应用程序是系统程序还是用户程序
直接上代码:
AppInfo.java
<span style="font-size:18px;">package com.example.packages; public class AppInfo { private String packName; private boolean userApp; public String getPackName() { return packName; } public void setPackName(String packName) { this.packName = packName; } public boolean isUserApp() { return userApp; } public void setUserApp(boolean userApp) { this.userApp = userApp; } }</span>
AppTools.java
<span style="font-size:18px;">package com.example.packages; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; public class AppTools { public static List<AppInfo> getAppInfos(Context context){ PackageManager pm = context.getPackageManager(); List<AppInfo> appInfos = new ArrayList<AppInfo>(); List<PackageInfo> packInfos = pm.getInstalledPackages(0); for(PackageInfo packInfo : packInfos){ AppInfo appInfo = new AppInfo(); String packName = packInfo.packageName; appInfo.setPackName(packName); int flags = packInfo.applicationInfo.flags;//标识 if((ApplicationInfo.FLAG_SYSTEM & flags) ==0){ //用户程序 appInfo.setUserApp(true); }else{ //系统程序 appInfo.setUserApp(false); } appInfos.add(appInfo); } return appInfos; } }</span>