首页 > 代码库 > 获取所有应用信息,判断系统应用或第三方应用,监听应用安装和卸载

获取所有应用信息,判断系统应用或第三方应用,监听应用安装和卸载

获取所有应用及相关信息


方法1:ResolveInfo

获取全部应用:
	PackageManager packageManager = getPackageManager();
	Intent mIntent = new Intent(Intent.ACTION_MAIN, null);
	mIntent.addCategory(Intent.CATEGORY_LAUNCHER);
	List<ResolveInfo> listAllApps = packageManager.queryIntentActivities(mIntent, 0);
判断是否系统应用:
	ResolveInfo appInfo = listAllApps.get(position);
	String pkgName = appInfo.activityInfo.packageName;//获取包名
	//根据包名获取PackageInfo mPackageInfo;(需要处理异常)
	mPackageInfo = mContext.getPackageManager().getPackageInfo(pkgName, 0);
	if ((mPackageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) {
		    //第三方应用
		} else {
		    //系统应用
		}
获取相关信息:
ResolveInfo mResolveInfo ;
获取包名: mResolveInfo.activityInfo.packageName;
获取icon: mResolveInfo.loadIcon(packageManager);
获取应用名: mResolveInfo.loadLabel(packageManager).toString();


方法2:PackageInfo

获取全部应用:
PackageManager packageManager = this.getPackageManager(); 
List<PackageInfo> packageInfoList = packageInfoList .getInstalledPackages(0);  
判断是否系统应用:
 List<PackageInfo> apps = new ArrayList<PackageInfo>();
 for (int i = 0; i < packageInfoList.size(); i++) {
            PackageInfo pak = (PackageInfo)packageInfoList.get(i);
            //判断是否为系统预装的应用
            if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {
                // 第三方应用
                apps.add(pak);
            } else
		{
		//系统应用
		}
        }
获取相关信息:
PackageInfo mPackageInfo;
获取包名: mPackageInfo.packageName
获取icon: mPackageInfo.getApplicationIcon(applicationInfo);
获取应用名: mPackageInfo.getApplicationLabel(applicationInfo);
获取使用权限:
mPackageInfo.getPackageInfo(packageName,PackageManager.GET_PERMISSIONS).requestedPermissions;

监听应用的安装和卸载:

//////////////注册广播,监听安装和卸载
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {
//获取安装应用的包名
String packageName = intent.getDataString();
} else if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {
//获取卸载应用的包名
String packageName = intent.getDataString();
}
}