001 | package lab.sodino.fetchapkicon; |
002 | |
003 | import java.io.File; |
004 | import java.lang.reflect.Constructor; |
005 | import java.lang.reflect.Field; |
006 | import java.lang.reflect.Method; |
007 | import android.app.Activity; |
008 | import android.content.Context; |
009 | import android.content.pm.ApplicationInfo; |
010 | import android.content.pm.PackageInfo; |
011 | import android.content.pm.PackageManager; |
012 | import android.content.res.Resources; |
013 | import android.graphics.drawable.Drawable; |
014 | import android.os.Bundle; |
015 | import android.util.DisplayMetrics; |
016 | import android.util.Log; |
017 | import android.view.View; |
018 | import android.widget.ImageView; |
019 | |
020 | public 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 | } |