I had nothing to do over the weekend. I took out the basic knowledge of Android and reviewed it. Today's topic is "getting uninstalled APK icons, versions, package names, names, whether to install, jump to install, open".
1. Get the APK icon
You can use getApplicationIcon (ApplicationInfo) in PackageManager to obtain a drawable. However, in actual use, you can only get one default icon, which is not an APK icon at all.
Refer to Xiaomi Open Source File Manager and use it in combination with practices. The Code is as follows:
/** A new method is used to obtain the APK icon. The previous failure is caused by a BUG in android. * appInfo is used. publicSourceDir = apkPath; To fix this problem, see: * http://code.google.com/p/android/issues/detail? Id = 9151 */public static Drawable getApkIcon (Context context, String apkPath) {PackageManager pm = context. getPackageManager (); PackageInfo = 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 ;}
In the following code segment, PackageManager, PackageInfo, and ApplicationInfo are all the same as above.
2. Get the APK name
String label = appInfo.loadLabel(mPackManager).toString();
3. Get the APK package name
String packageName = appInfo.packageName;
4. Obtain the APK version
String version = info.versionName==null?"0":info.versionName
5. Determine whether the APK is installed
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; } }
6. Install 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); }
7. Open APK
</pre><pre name="code" class="java"> private void openAPK(String packagename) { PackageManager packageManager = mContext.getPackageManager(); Intent intent=new Intent(); intent =packageManager.getLaunchIntentForPackage(packagename); mContext.startActivity(intent); }
Ps. The Silent Installation of APK is still under study...