android获取应用信息,判断应用来源,监听应用的安装和卸载

转载地址:http://blog.csdn.net/maoxiao1229/article/details/38301657

转载的目的,一个是方便自己学习,也为了让更多人看到学习

获取所有应用及相关信息

方法1:ResolveInfo


    获取全部应用:  
        PackageManager packageManager = getPackageManager();  
        Intent mIntent = new Intent(Intent.ACTION_MAIN, null);  
        mIntent.addCategory(Intent.CATEGORY_LAUNCHER);  
        List 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 packageInfoList = packageInfoList .getInstalledPackages(0);    
    判断是否系统应用:  
     List apps = new ArrayList();  
     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();  
    }  
    }  


你可能感兴趣的:(Android,监听,系统监听及其他)