Weekend free to work, the basic knowledge of Android to review review, today's theme is "Get the apk icon, version, package name, name, whether install, jump install, open"
first, get the apk icon
Usually the icon to read the APK can be used, Packagemanager inside the Getapplicationicon (applicationinfo) to get a drawable. But the actual use often can only get a default icon, is not the apk icon.
Refer to Xiaomi Open source file Manager, combined with practice, the code is as follows:
/* * A new way to get the apk icon, the previous failure is due to a bug in Android, through * Appinfo.publicsourcedir = Apkpath, to fix this problem, see the details: * http://code.google.com/p/android/issues/detail?id=9151 * /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; }
The following code snippet Packagemanager, PackageInfo, and ApplicationInfo are all consistent with the above.
second, get the APK name
String label = Appinfo.loadlabel (Mpackmanager). toString ();
third, get the APK package name
String PackageName = appinfo.packagename;
Iv. get APK version
String Version = Info.versionname==null? " 0 ": Info.versionname
v. To determine if 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; } }
vi. Install APK
private void installapk (String apkpath) {Intent Intent = new Intent (); intent.setaction (Intent.action_view); INTENT.ADDF Lags (Intent.flag_activity_new_task); Intent.setdataandtype (Uri.parse ("file://" + Apkpath), "application/vnd.android.package-archive"); Mcontext.startactivity (Intent); }
Seven, 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. Silent installation of APK is still under study ...