Android Rookie note-get an installed apk icon, version number, package name, name, install, install, open

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.