First, get installation package information
Copy Code code as follows:
/**
* Get APK Package Information: Version number, name, icon, etc.
* @param the absolute path of the Abspath APK package
* @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;
/* Must add these two sentences, or the following icon to get the default icon instead of the application package icon * *
Appinfo.sourcedir = Abspath;
Appinfo.publicsourcedir = Abspath;
String appName = Pm.getapplicationlabel (appInfo). toString ();//Get Application Name
String PackageName = appinfo.packagename; Get the package name
String Version = Pkginfo.versionname; Get version Information
* Icon1 and Icon2 are actually the same * *
drawable Icon1 = Pm.getapplicationicon (appInfo)//Get icon information
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));
}
}
Second, in the APK installation, we can obtain installation package, version, package name and other information.
Copy Code code as follows:
public class Testactivity extends activity {
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
String archivefilepath= "sdcard/jb51.net.apk";//install package path
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; Get installation package Name
String Version=info.versionname; Get version Information
Toast.maketext (Testactivity.this, Toast.length_long). Show ();
drawable icon = Pm.getapplicationicon (appInfo);//Get icons Information
TextView TV = (TextView) Findviewbyid (r.id.tv);
Tv.settext ("AppName:" +appname+ "---packagename:" +packagename);
Display icon
ImageView tu= (ImageView) Findviewbyid (R.ID.IMAGEVIEW1);
Tu.setbackgrounddrawable (icon);
}
}
}
}
}