Android对手机上已安装apk的常用操作

获取手机上已安装Apk的Context的方法

context.createPackageContext(packageName,flags);

packageName: 包名
flags: 
1.packageName  包名,要得到Context的包名
2.flags  标志位,有CONTEXT_INCLUDE_CODE和CONTEXT_IGNORE_SECURITY两个选项。
              CONTEXT_INCLUDE_CODE的意思是包括代码,也就是说可以执行这个包里面的代码。
              CONTEXT_IGNORE_SECURITY的意思是忽略安全警告,如果不加这个标志的话,有些功能是用不了的,会出现安全警告。

获取手机上安装的App信息

PackageManager pm = getPackageManager();
List installedPackages = pm.getInstalledPackages(PackageManager.GET_META_DATA);

获取其他App的AndroidManifest.xml中Application节点下meta-data数据

ApplicationInfo appInfo = packageInfo.applicationInfo;
String meta = appInfo.metaData.getString("EZWEATHER_PLUGIN");

获取手机上已安装的其他Apk资源

Resources resources = pm.getResourcesForApplication(packageName); 
int id = resources.getIdentifier("preview_image", "drawable", appInfo.packageName);

总结

PackageManager pm = getPackageManager();
List installedPackages = pm.getInstalledPackages(PackageManager.GET_META_DATA);
for (PackageInfo packageInfo : installedPackages) {
    ApplicationInfo appInfo = packageInfo.applicationInfo;
    if (null != packageInfo && null != appInfo.metaData) {
        String meta = appInfo.metaData.getString("WEATHER_PLUGIN");

        if ("mobi.infolife.ezweather.plugin.widget".equals(meta)) {
            Log.d(TAG, "找到了此插件");
            Log.d(TAG, "包名为" + packageInfo.packageName);
            try {
                Resources resources = pm.getResourcesForApplication(appInfo);
                int id = resources.getIdentifier("preview_image", "drawable", appInfo.packageName);
                tv.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(this, id), null, null, null);
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }

        }
    }

}

你可能感兴趣的:(Android对手机上已安装apk的常用操作)