Method in Android to get information about the apk installation package
- 2020-05-30 21:03:15
- OfStack
1. Get installation package information
/**
* To obtain apk Package information: version number, name, icon, etc
* @param absPath apk The absolute path of the 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;
/* These two sentences must be added, or the following icon Acquisition is default icon It's not application-packed icon */
appInfo.sourceDir = absPath;
appInfo.publicSourceDir = absPath;
String appName = pm.getApplicationLabel(appInfo).toString();// Get the applied name
String packageName = appInfo.packageName; // Get the package name
String version = pkgInfo.versionName; // Get version information
/* icon1 and icon2 Is actually 1 The sample of */
Drawable icon1 = pm.getApplicationIcon(appInfo);// Get the 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));
}
}
2. When installing APK, we can get the installation package, version, package name and other information.
public class TestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String archiveFilePath="sdcard/ofstack.com.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; // Gets the name of the installation package
String version=info.versionName; // Get version information
// Toast.makeText(TestActivity.this, , Toast.LENGTH_LONG).show();
Drawable icon = pm.getApplicationIcon(appInfo);// Get the icon 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);
}
}
}
}
}