파일 관리자 - apk 설치 패키지에 대한 정보 얻기
//
// apk
public static ApplicationInfo getApplicationInfo(Context context,String archiveFilePath) {
String PACKAGE_PARSER_CLASS_NAME = "android.content.pm.PackageParser";
ApplicationInfo applicationInfo = null;
try {
// PackageParser class
Class<?> packageParserClass = Class.forName(PACKAGE_PARSER_CLASS_NAME);
Class<?>[] parameterTypes = new Class[1];
parameterTypes[0] = String.class;
//
Constructor<?> packageParserConstructor = packageParserClass.getConstructor(parameterTypes);
Object[] args = new Object[1];
//PackageParser
args[0] = archiveFilePath;
// packageparser
Object packageParser = packageParserConstructor.newInstance(args);
Resources pRes = context.getResources();
parameterTypes = new Class[4];
parameterTypes[0] = File.class;
parameterTypes[1] = String.class;
parameterTypes[2] = pRes.getDisplayMetrics().getClass();
parameterTypes[3] = Integer.TYPE;
// parsePackage , public Package parsePackage(File sourceFile, String destCodePath,
//DisplayMetrics metrics, int flags)
Method parsePackage = packageParserClass.getDeclaredMethod("parsePackage", parameterTypes);
args = new Object[4];
args[0] = new File(archiveFilePath);
args[1] = archiveFilePath;
args[2] = pRes.getDisplayMetrics();
args[3] = 0;
Object packageInfo = parsePackage.invoke(packageParser, args);
//
Field applicationInfoField = packageInfo.getClass().getDeclaredField("applicationInfo");
applicationInfo = (ApplicationInfo) applicationInfoField.get(packageInfo);
} catch (Exception e) {
e.printStackTrace();
}
return applicationInfo;
}
// apk
public static Resources getResource(Context context, String archiveFilePath) {
String ASSET_MANAGER_CLASS_NAME = "android.content.res.AssetManager";
Resources resources = null;
try {
Class<?> assetManagerClass = Class.forName(ASSET_MANAGER_CLASS_NAME);
Constructor<?> assetManagerConstructor = assetManagerClass.getConstructor((Class[]) null);
Object assetManager = assetManagerConstructor.newInstance((Object[]) null);
Class<?>[] parameterTypes = new Class[1];
parameterTypes[0] = String.class;
Method addAssetPath = assetManagerClass.getDeclaredMethod("addAssetPath", parameterTypes);
Object[] args = new Object[1];
args[0] = archiveFilePath;
addAssetPath.invoke(assetManager, args);
Resources pRes = context.getResources();
parameterTypes = new Class[3];
parameterTypes[0] = assetManager.getClass();
parameterTypes[1] = pRes.getDisplayMetrics().getClass();
parameterTypes[2] = pRes.getConfiguration().getClass();
Constructor<?> resourcesConstructor = Resources.class.getConstructor(parameterTypes);
args = new Object[3];
args[0] = assetManager;
args[1] = pRes.getDisplayMetrics();
args[2] = pRes.getConfiguration();
resources = (Resources) resourcesConstructor.newInstance(args);
} catch (Exception e) {
e.printStackTrace();
}
return resources;
}
/**
* apk
* @param context
* @param file
* @return
*/
public static Drawable getApkIcon(Context context, File file) {
Drawable icon = null;
PackageManager pm = context.getPackageManager();
ApplicationInfo applicationInfo = getApplicationInfo(context, file.getPath());
Resources resources = getResource(context, file.getPath());
if (applicationInfo == null || resources == null) {
icon = pm.getDefaultActivityIcon();
} else{
if (applicationInfo.icon != 0) {
try {
icon = resources.getDrawable(applicationInfo.icon);
} catch (Resources.NotFoundException e) {
}
}
if (icon == null) {
icon = pm.getDefaultActivityIcon();
}
}
int iconSize = context.getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
icon.setBounds(0, 0, iconSize, iconSize);
return icon;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Exception Class에서 에러 코드 해석 ~초기초편~직장에서 C# 프로젝트가 내뿜는 오류 코드를 구문 분석하고 오류의 위치를 확인하기 위해 Exception class를 활용할 수 있었습니다. 지금까지 Exception Class 에 대해서 별로 파악할 수 없었기 때...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.