获取未安装的APK图标

 

001package lab.sodino.fetchapkicon;
002  
003import java.io.File;
004import java.lang.reflect.Constructor;
005import java.lang.reflect.Field;
006import java.lang.reflect.Method;
007import android.app.Activity;
008import android.content.Context;
009import android.content.pm.ApplicationInfo;
010import android.content.pm.PackageInfo;
011import android.content.pm.PackageManager;
012import android.content.res.Resources;
013import android.graphics.drawable.Drawable;
014import android.os.Bundle;
015import android.util.DisplayMetrics;
016import android.util.Log;
017import android.view.View;
018import android.widget.ImageView;
019  
020public class FetchAPKIconAct extends Activity {
021 public void onCreate(Bundle savedInstanceState) {
022  super.onCreate(savedInstanceState);
023  setContentView(R.layout.main);
024  showUninstallAPKIcon("/sdcard/APK/JarodYv.FishPool.apk");
025  getUninatllApkInfo(this, "/sdcard/APK/JarodYv.FishPool.apk");
026 }
027   
028 public void getUninatllApkInfo(Context context, String archiveFilePath) {
029  PackageManager pm = context.getPackageManager();
030  PackageInfo info = pm.getPackageArchiveInfo(archiveFilePath, PackageManager.GET_ACTIVITIES);
031  if (info != null) {
032   ApplicationInfo appInfo = info.applicationInfo;
033   Drawable icon = pm.getApplicationIcon(appInfo);
034   ImageView image = (ImageView) findViewById(R.id.apkIconByTradition);
035   image.setVisibility(View.VISIBLE);
036   image.setImageDrawable(icon);
037  }
038 }
039 //
040 private void showUninstallAPKIcon(String apkPath) {
041  String PATH_PackageParser = "android.content.pm.PackageParser";
042  String PATH_AssetManager = "android.content.res.AssetManager";
043  try {
044   // apk包的文件路径
045   // 这是一个Package 解释器, 是隐藏的
046   // 构造函数的参数只有一个, apk文件的路径
047   // PackageParser packageParser = new PackageParser(apkPath);
048   Class pkgParserCls = Class.forName(PATH_PackageParser);
049   Class[] typeArgs = new Class[1];
050   typeArgs[0] = String.class;
051   Constructor pkgParserCt = pkgParserCls.getConstructor(typeArgs);
052   Object[] valueArgs = new Object[1];
053   valueArgs[0] = apkPath;
054   Object pkgParser = pkgParserCt.newInstance(valueArgs);
055   Log.d("ANDROID_LAB", "pkgParser:" + pkgParser.toString());
056   // 这个是与显示有关的, 里面涉及到一些像素显示等等, 我们使用默认的情况
057   DisplayMetrics metrics = new DisplayMetrics();
058   metrics.setToDefaults();
059   // PackageParser.Package mPkgInfo = packageParser.parsePackage(new
060   // File(apkPath), apkPath,
061   // metrics, 0);
062   typeArgs = new Class[4];
063   typeArgs[0] = File.class;
064   typeArgs[1] = String.class;
065   typeArgs[2] = DisplayMetrics.class;
066   typeArgs[3] = Integer.TYPE;
067   Method pkgParser_parsePackageMtd = pkgParserCls.getDeclaredMethod("parsePackage",
068     typeArgs);
069   valueArgs = new Object[4];
070   valueArgs[0] = new File(apkPath);
071   valueArgs[1] = apkPath;
072   valueArgs[2] = metrics;
073   valueArgs[3] = 0;
074   Object pkgParserPkg = pkgParser_parsePackageMtd.invoke(pkgParser, valueArgs);
075   // 应用程序信息包, 这个公开的, 不过有些函数, 变量没公开
076   // ApplicationInfo info = mPkgInfo.applicationInfo;
077   Field appInfoFld = pkgParserPkg.getClass().getDeclaredField("applicationInfo");
078   ApplicationInfo info = (ApplicationInfo) appInfoFld.get(pkgParserPkg);
079   // uid 输出为"-1",原因是未安装,系统未分配其Uid。
080   Log.d("ANDROID_LAB", "pkg:" + info.packageName + " uid=" + info.uid);
081   // Resources pRes = getResources();
082   // AssetManager assmgr = new AssetManager();
083   // assmgr.addAssetPath(apkPath);
084   // Resources res = new Resources(assmgr, pRes.getDisplayMetrics(),
085   // pRes.getConfiguration());
086   Class assetMagCls = Class.forName(PATH_AssetManager);
087   Constructor assetMagCt = assetMagCls.getConstructor((Class[]) null);
088   Object assetMag = assetMagCt.newInstance((Object[]) null);
089   typeArgs = new Class[1];
090   typeArgs[0] = String.class;
091   Method assetMag_addAssetPathMtd = assetMagCls.getDeclaredMethod("addAssetPath",
092     typeArgs);
093   valueArgs = new Object[1];
094   valueArgs[0] = apkPath;
095   assetMag_addAssetPathMtd.invoke(assetMag, valueArgs);
096   Resources res = getResources();
097   typeArgs = new Class[3];
098   typeArgs[0] = assetMag.getClass();
099   typeArgs[1] = res.getDisplayMetrics().getClass();
100   typeArgs[2] = res.getConfiguration().getClass();
101   Constructor resCt = Resources.class.getConstructor(typeArgs);
102   valueArgs = new Object[3];
103   valueArgs[0] = assetMag;
104   valueArgs[1] = res.getDisplayMetrics();
105   valueArgs[2] = res.getConfiguration();
106   res = (Resources) resCt.newInstance(valueArgs);
107   CharSequence label = null;
108   if (info.labelRes != 0) {
109    label = res.getText(info.labelRes);
110   }
111   // if (label == null) {
112   // label = (info.nonLocalizedLabel != null) ? info.nonLocalizedLabel
113   // : info.packageName;
114   // }
115   Log.d("ANDROID_LAB", "label=" + label);
116   // 这里就是读取一个apk程序的图标
117   if (info.icon != 0) {
118    Drawable icon = res.getDrawable(info.icon);
119    ImageView image = (ImageView) findViewById(R.id.apkIconBySodino);
120    image.setVisibility(View.VISIBLE);
121    image.setImageDrawable(icon);
122   }
123  } catch (Exception e) {
124   e.printStackTrace();
125  }
126 }
127}
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REaDME.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 、资源1项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值