您的位置:首页 > 其它

获取未安装的APK图标

2012-04-23 23:39 281 查看


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(BundlesavedInstanceState){
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(Contextcontext,StringarchiveFilePath){
029
PackageManagerpm=context.getPackageManager();
030
PackageInfoinfo=pm.getPackageArchiveInfo(archiveFilePath,PackageManager.GET_ACTIVITIES);
031
if

(info!=
null
){
032
ApplicationInfoappInfo=info.applicationInfo;
033
Drawableicon=pm.getApplicationIcon(appInfo);
034
ImageViewimage=(ImageView)findViewById(R.id.apkIconByTradition);
035
image.setVisibility(View.VISIBLE);
036
image.setImageDrawable(icon);
037
}

038
}

039
//

040
private

void
showUninstallAPKIcon(StringapkPath){
041
StringPATH_PackageParser=
"android.content.pm.PackageParser"
;
042
StringPATH_AssetManager=
"android.content.res.AssetManager"
;
043
try

{
044
//apk包的文件路径
045
//这是一个Package解释器,是隐藏的
046
//构造函数的参数只有一个,apk文件的路径
047
//PackageParserpackageParser=newPackageParser(apkPath);
048
ClasspkgParserCls=Class.forName(PATH_PackageParser);
049
Class[]typeArgs=
new
Class[
1
];
050
typeArgs[
0
]=String.
class
;
051
ConstructorpkgParserCt=pkgParserCls.getConstructor(typeArgs);
052
Object[]valueArgs=
new
Object[
1
];
053
valueArgs[
0
]=apkPath;
054
ObjectpkgParser=pkgParserCt.newInstance(valueArgs);
055
Log.d(
"ANDROID_LAB"
,
"pkgParser:"
+pkgParser.toString());
056
//这个是与显示有关的,里面涉及到一些像素显示等等,我们使用默认的情况
057
DisplayMetricsmetrics=
new
DisplayMetrics();
058
metrics.setToDefaults();
059
//PackageParser.PackagemPkgInfo=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
MethodpkgParser_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
ObjectpkgParserPkg=pkgParser_parsePackageMtd.invoke(pkgParser,valueArgs);
075
//应用程序信息包,这个公开的,不过有些函数,变量没公开
076
//ApplicationInfoinfo=mPkgInfo.applicationInfo;
077
FieldappInfoFld=pkgParserPkg.getClass().getDeclaredField(
"applicationInfo"
);
078
ApplicationInfoinfo=(ApplicationInfo)appInfoFld.get(pkgParserPkg);
079
//uid输出为"-1",原因是未安装,系统未分配其Uid。
080
Log.d(
"ANDROID_LAB"
,
"pkg:"
+info.packageName+
"uid="
+info.uid);
081
//ResourcespRes=getResources();
082
//AssetManagerassmgr=newAssetManager();
083
//assmgr.addAssetPath(apkPath);
084
//Resourcesres=newResources(assmgr,pRes.getDisplayMetrics(),
085
//pRes.getConfiguration());
086
ClassassetMagCls=Class.forName(PATH_AssetManager);
087
ConstructorassetMagCt=assetMagCls.getConstructor((Class[])
null
);
088
ObjectassetMag=assetMagCt.newInstance((Object[])
null
);
089
typeArgs=
new
Class[
1
];
090
typeArgs[
0
]=String.
class
;
091
MethodassetMag_addAssetPathMtd=assetMagCls.getDeclaredMethod(
"addAssetPath"
,
092
typeArgs);
093
valueArgs=
new
Object[
1
];
094
valueArgs[
0
]=apkPath;
095
assetMag_addAssetPathMtd.invoke(assetMag,valueArgs);
096
Resourcesres=getResources();
097
typeArgs=
new
Class[
3
];
098
typeArgs[
0
]=assetMag.getClass();
099
typeArgs[
1
]=res.getDisplayMetrics().getClass();
100
typeArgs[
2
]=res.getConfiguration().getClass();
101
ConstructorresCt=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
CharSequencelabel=
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
Drawableicon=res.getDrawable(info.icon);
119
ImageViewimage=(ImageView)findViewById(R.id.apkIconBySodino);
120
image.setVisibility(View.VISIBLE);
121
image.setImageDrawable(icon);
122
}

123
}

catch
(Exceptione){

124
e.printStackTrace();
125
}

126
}

127
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: