Android gets information on installed and not installed APK

Source: Internet
Author: User

One of the most important classes in getting the APK resources is Packagemanager, which allows us to get all the things we want, starting with the basic information of the APK that has been installed, including the label, the icon, and other resources:

Packagemanager pm = Getpackagemanager (); list<packageinfo> Apkinfos = pm.getinstalledpackages (0); arraylist<string> infos_name =  new arraylist<string> ()//for storing labelarraylist<drawable> Infos_ icon = newarraylist<drawable> ();//used to store the APK boot icon resource string name = "";   drawable icon;   PackageInfo apk;   for (int i = 0; i < apkinfos.size ();)   {      apk = Apkinfos.get (i);      i++;      Name = (String) Pm.getapplicationlabel (apk.applicationinfo);      icon = Pm.getapplicationicon (apk.applicationinfo);      Infos_icon.add (icon);      Infos_name.add (name);   }
Next we use Packagemanager to get the activity in the installed APK, and the service, etc.

List<packageinfo>packagesinfo = pm.getinstalledpackages (0); for (PackageInfo Packageinfo:packagesinfo) {LOG.D ( "TAG", "PackageInfo NAME is:" +packageinfo.packagename); Packagemanager pm = Getpackagemanager (); try {// Get the Activity,service and broadcastreceiverpackageinfo PackageInfo = Pm.getpackageinfo in the Com.example.pertest package (" Com.example.pertest ", packagemanager.get_permissions| packagemanager.get_services| Packagemanager.get_receivers); Activityinfo[] Activities = PACKAGEINFO.ACTIVITIES;IF (activities! = null) {for (Activityinfo activityinfo:activities) { LOG.D ("TAG", "the Activity" + activityinfo.tostring () + "= =" + Packageinfo.versioncode + "= = =" + Packageinfo.versionname); }} else {log.d ("TAG", "The activity is null");} serviceinfo[] Serviceinfos = packageinfo.services;for (serviceinfo serviceinfo:serviceinfos) {Log.d ("TAG", "service Name is: "+ serviceinfo.name);} Activities = Packageinfo.receivers;for (Activityinfo activity:activities) {log.d ("TAG", "Receiver is:" + activity.name);}} catch (Namenotfoundexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}

The above is the basic information to get the installed APK, and then see how to get the basic information of the APK file is not installed, which is useful for Android dynamic loading, we look at:

Get the icon for the APK not installed:

public static drawable Getappicon (context context, String Apkfilepath) {        Packagemanager pm = Context.getpackagemanager ();        PackageInfo PkgInfo = getpackageinfo (context, apkfilepath);        if (PkgInfo = = null) {            return null;        }        ApplicationInfo appInfo = pkginfo.applicationinfo;        if (Build.VERSION.SDK_INT >= 8) {            appinfo.sourcedir = Apkfilepath;            Appinfo.publicsourcedir = Apkfilepath;        }        Return Pm.getapplicationicon (AppInfo);    }
Get the name of the APK that is not installed:

public static Charsequence Getapplabel (context context, String Apkfilepath) {        Packagemanager pm = Context.getpackagemanager ();        PackageInfo PkgInfo = getpackageinfo (context, apkfilepath);        if (PkgInfo = = null) {            return null;        }        ApplicationInfo appInfo = pkginfo.applicationinfo;        if (Build.VERSION.SDK_INT >= 8) {            appinfo.sourcedir = Apkfilepath;            Appinfo.publicsourcedir = Apkfilepath;        }        Return Pm.getapplicationlabel (AppInfo); }
If it's not enough, we can get: PackageInfo object, through which we can get the activity,service,broadcastreceiver of the not installed APK, and so on, the same way as "get activity in installed APK" , and service ", the code is as follows:

Gets the PackageInfo object that contains the activity and Servicepublic static PackageInfo Getpackageinfo (context context, String Apkfilepath) {        Packagemanager pm = Context.getpackagemanager ();        PackageInfo pkgInfo = null;        try {            pkgInfo = Pm.getpackagearchiveinfo (Apkfilepath, Packagemanager.get_activities | packagemanager.get_services);        } catch (Exception e) {            //should is something wrong with parse            e.printstacktrace ();        }        return pkgInfo;    }

When Android is loaded dynamically, it is implemented by Dexclassloader, which is the way to get Dexclassloader objects for which the program is not installed:

String Mnativelibdir = Mcontext.getdir ("Pluginlib", Context.mode_private). GetAbsolutePath (); Private Dexclassloader Createdexclassloader (String dexpath) {        File Dexoutputdir = Mcontext.getdir ("Dex", Context.mode_private);        Dexoutputpath = Dexoutputdir.getabsolutepath ();        Dexclassloader loader = new Dexclassloader (Dexpath, Dexoutputpath, Mnativelibdir, Mcontext.getclassloader ());        return loader; }
The first parameter, Dexpath, is the storage path of our apk.

The second parameter, Dexoutpath, is the stored path of the Dex file that corresponds to the apk file, not null.

The third parameter is a list of C + + libraries used in the target class, with each directory File.pathSeparator间隔开 ;null。

  第四个参数是该类装载器的父装载器,一般用当前执行类的装载器。

在获得了该DexClassLoader对象以后,我们就可以实现动态加载该apk中的方法了,详细的方法,请看我的另一篇文章:android动态加载

对于加载未安装的apk中的资源,由于使用的是不同的context,所以,这里先这样做:

Private Assetmanager Createassetmanager (String dexpath) {        try {            Assetmanager Assetmanager = AssetManager.class.newInstance ();            Method Addassetpath = Assetmanager.getclass (). GetMethod ("Addassetpath", string.class);            Addassetpath.invoke (Assetmanager, dexpath);            return assetmanager;        } catch (Exception e) {            e.printstacktrace ();            return null;        }    } Private Resources createresources (Assetmanager assetmanager) {        Resources superres = mcontext.getresources ();        Resources Resources = new resources (Assetmanager, Superres.getdisplaymetrics (), superres.getconfiguration ());        return resources;}
At this time the Assetmanager and resources can be used as usual.







Android gets information on installed and not installed APK

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.