一、擷取APK圖示
public static Drawable getApkIcon(Context context, String apkPath) {
PackageManager pm = context.getPackageManager();
PackageInfo info = pm.getPackageArchiveInfo(apkPath,
PackageManager.GET_ACTIVITIES);
if (info != null) {
ApplicationInfo appInfo = info.applicationInfo;
appInfo.sourceDir = apkPath;
appInfo.publicSourceDir = apkPath;
try {
return appInfo.loadIcon(pm);
} catch (OutOfMemoryError e) {
Log.e("ApkIconLoader", e.toString());
}
}
return null;
}
下面代碼段中PackageManager、PackageInfo、ApplicationInfo均同上面一緻。
二、擷取APK名稱
String label = appInfo.loadLabel(mPackManager).toString();
三、擷取APK包名
String packageName = appInfo.packageName;
四、擷取APK版本号
五、推斷APK是否安裝
private boolean isApkInstalled(String packagename)
{
PackageManager localPackageManager = getPackageManager();
try
{
PackageInfo localPackageInfo = localPackageManager.getPackageInfo(packagename, PackageManager.GET_UNINSTALLED_PACKAGES);
return true;
}
catch (PackageManager.NameNotFoundException localNameNotFoundException)
{
return false;
}
}
六、安裝APK
private void installAPK(String apkPath)
{
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.parse("file://" + apkPath),
"application/vnd.android.package-archive");
mContext.startActivity(intent);
}
七、打開APK
private void openAPK(String packagename)
{
PackageManager packageManager = mContext.getPackageManager();
Intent intent=new Intent();
intent =packageManager.getLaunchIntentForPackage(packagename);
mContext.startActivity(intent);
}