Android中获取apk安装包信息的方法
程序员文章站
2023-08-29 19:04:51
一、获取安装包信息
复制代码 代码如下:/** * 获取apk包的信息:版本号,名称,图标等 * @param abspath apk包的绝对路径 * @param co...
一、获取安装包信息
复制代码 代码如下:
/**
* 获取apk包的信息:版本号,名称,图标等
* @param abspath apk包的绝对路径
* @param context
*/
public void apkinfo(string abspath,context context) {
packagemanager pm = context.getpackagemanager();
packageinfo pkginfo = pm.getpackagearchiveinfo(abspath,packagemanager.get_activities);
if (pkginfo != null) {
applicationinfo appinfo = pkginfo.applicationinfo;
/* 必须加这两句,不然下面icon获取是default icon而不是应用包的icon */
appinfo.sourcedir = abspath;
appinfo.publicsourcedir = abspath;
string appname = pm.getapplicationlabel(appinfo).tostring();// 得到应用名
string packagename = appinfo.packagename; // 得到包名
string version = pkginfo.versionname; // 得到版本信息
/* icon1和icon2其实是一样的 */
drawable icon1 = pm.getapplicationicon(appinfo);// 得到图标信息
drawable icon2 = appinfo.loadicon(pm);
string pkginfostr = string.format("packagename:%s, vesion: %s, appname: %s", packagename, version, appname);
log.i(tag, string.format("pkginfo: %s", pkginfostr));
}
}
* 获取apk包的信息:版本号,名称,图标等
* @param abspath apk包的绝对路径
* @param context
*/
public void apkinfo(string abspath,context context) {
packagemanager pm = context.getpackagemanager();
packageinfo pkginfo = pm.getpackagearchiveinfo(abspath,packagemanager.get_activities);
if (pkginfo != null) {
applicationinfo appinfo = pkginfo.applicationinfo;
/* 必须加这两句,不然下面icon获取是default icon而不是应用包的icon */
appinfo.sourcedir = abspath;
appinfo.publicsourcedir = abspath;
string appname = pm.getapplicationlabel(appinfo).tostring();// 得到应用名
string packagename = appinfo.packagename; // 得到包名
string version = pkginfo.versionname; // 得到版本信息
/* icon1和icon2其实是一样的 */
drawable icon1 = pm.getapplicationicon(appinfo);// 得到图标信息
drawable icon2 = appinfo.loadicon(pm);
string pkginfostr = string.format("packagename:%s, vesion: %s, appname: %s", packagename, version, appname);
log.i(tag, string.format("pkginfo: %s", pkginfostr));
}
}
二、在apk安装的时候,我们可以获取安装包、版本、包名等信息。
复制代码 代码如下:
public class testactivity extends activity {
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
string archivefilepath="sdcard/jb51.net.apk";//安装包路径
packagemanager pm = getpackagemanager();
packageinfo info = pm.getpackagearchiveinfo(archivefilepath, packagemanager.get_activities);
if(info != null){
applicationinfo appinfo = info.applicationinfo;
string appname = pm.getapplicationlabel(appinfo).tostring();
string packagename = appinfo.packagename; //得到安装包名称
string version=info.versionname; //得到版本信息
// toast.maketext(testactivity.this, , toast.length_long).show();
drawable icon = pm.getapplicationicon(appinfo);//得到图标信息
textview tv = (textview)findviewbyid(r.id.tv);
tv.settext("appname:"+appname+"---packagename:"+packagename);
//显示图标
imageview tu=(imageview)findviewbyid(r.id.imageview1);
tu.setbackgrounddrawable(icon);
}
}
}
}
}