Weekend idle to have no matter, the basic knowledge of Android to review review, today's theme is "Get the apk icon, version number, 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. However, when used in practice, it is often only possible to get a default icon. It's not the apk icon at all.
The Xiaomi open source file Manager, combined with practice, code such as the following:
/ * * A new way to get the APK icon is used. 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 the APK version number
String Version = Info.versionname==null?"0": info.versionname
v. Infer 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 ...
Android Rookie note-get an installed apk icon, version number, package name, name, install, install, open