android 获取一个apk的信息
2010-11-05 00:55:01 来源:WEB开发网最近在看packageinstaller的ap,对面内容进行总结做一下学习笔记,下面是获取一个apk的application信息的代码:
Java代码
/*
* Utility method to get application information for a given packageURI
*/
public static ApplicationInfo getApplicationInfo(Uri packageURI) {
final String archiveFilePath = packageURI.getPath();
PackageParser packageParser = new PackageParser(archiveFilePath);
File sourceFile = new File(archiveFilePath);
DisplayMetrics metrics = new DisplayMetrics();
metrics.setToDefaults();
PackageParser.Package pkg = packageParser.parsePackage(sourceFile, archiveFilePath, metrics, 0);
if (pkg == null) {
return null;
}
return pkg.applicationInfo;
}
/*
* Utility method to get application information for a given packageURI
*/
public static ApplicationInfo getApplicationInfo(Uri packageURI) {
final String archiveFilePath = packageURI.getPath();
PackageParser packageParser = new PackageParser(archiveFilePath);
File sourceFile = new File(archiveFilePath);
DisplayMetrics metrics = new DisplayMetrics();
metrics.setToDefaults();
PackageParser.Package pkg = packageParser.parsePackage(sourceFile, archiveFilePath, metrics, 0);
if (pkg == null) {
return null;
}
return pkg.applicationInfo;
}
这里的Uri的参数是可以是这样形成的:
Creates a Uri from a file. The URI has the form "file://". Encodes path characters with the exception of '/'.
Example: "file:///tmp/android.txt"
Java代码
String fileName="test.apk";
Uri.fromFile(new File(fileName));
更多精彩
赞助商链接