"Turn" Android essay--packagemanager detailed

Source: Internet
Author: User

Reference: http://www.cnblogs.com/xingfuzzhd/p/3374504.html

Today is to talk about the Packagemanager. The Android system provides us with a wide range of service management classes, including Activitymanager, PowerManager (Power Management), Audiomanager (audio Management), and more. In addition to this, a Packagemanger management class is provided, whose primary responsibility is to manage application packages. With Packagemanager, we can get application information.

When it comes to Packagemanager, you have to mention the Androidmanifest.xml file. Androidmanifest.xml is one of the most important files in Android apps. It is the global profile of the Android program and is the file that must be in each Android program. It is located at the root of the application we develop, and describes the global data in the package, including the components exposed in the packages (activities, services, and so on), their respective implementation classes, the various data that can be processed, and the location of the boot, and other important information.
Therefore, the file provides the necessary information about the application that the Android system needs, that is, the information that the system must have before any code for the application is run.

The information obtained by Packagemanager is from Androidmanifest.xml. For ease of understanding, from the Internet to find a anroidmanifest.xml file Node description diagram:

First, the function of Packagemanager:

1, install, uninstall the application
2, inquiry permission related information
3, Query application related information (application,activity,receiver,service,provider and corresponding attributes, etc.)
4. Query installed Apps
5, add, delete permission
6. Clear user data, cache, code snippets, etc.

Second, Packagemanager related classes and methods introduced:

1, Packagemanager class

Description: Gets the installed application information. Can be obtained through the Getpackagemanager () method. Common methods: Public abstract Packagemanager Getpackagemanager () function: Get a Packagemanger object public abstract drawable getapplication Icon (String packagename) parameter: PackageName Package name function: Returns the icon for the given package name, otherwise returns nullpublic abstract ApplicationInfo Getapplicationinfo ( String PackageName, int flags) parameter: PackageName Package name Flags The APPLICATIONINFO is this flags tag, It is usually possible to give a constant 0 function directly: Return the ApplicationInfo object public abstract list<applicationinfo> getinstalledapplications (int flags ) parameter: Flag is generally get_uninstalled_packages, then all ApplicationInfo are returned. We can filter on ApplicationInfo's flags and get what we need. Function: Returns all Packageinfopublic abstract list<packageinfo> getinstalledpackages (int flags) for a given condition Parameters as above function: Returns all Packageinfopublic abstract ResolveInfo resolveactivity (Intent Intent, int flags) parameters for the given condition: Intent search condition, Activi     The action and category flags:MATCH_DEFAULT_ONLY:Category configured by Ty must have category_default activity to match get_intent_filters : Match Intent condition to Get_resolved_filter: Match intent condition to function: Returns Resolveinf for a given conditiono Object (essentially activity) public abstract list<resolveinfo> queryintentactivities (Intent Intent, int flags) parameter ditto function : Returns all ResolveInfo objects of a given condition (essentially activity), collection object public abstract ResolveInfo resolveservice (Intent Intent, int flags) parameter ditto function : Returns the ResolveInfo object for the given condition (essentially a service) public abstract list<resolveinfo> queryintentservices (Intent Intent, int Flags) Parameter: Returns all Resolveinfo objects of a given condition (essentially service), collection object

2, Packageiteminfo class

Description: The base class for all nodes in the Androidmanifest.xml file provides basic information about these nodes: label, icon, Meta-data. It is not used directly, but is inherited by subclasses and then called accordingly.

3. ApplicationInfo class inherits from Packageiteminfo class

Description: Gets information about the <application> node in a specific reference program. Field Description:        flags field: Flag_system System Application                    Flag_external_storage indicates that the app is installed in SDcard common method inherits from LoadIcon in the Packageiteminfo class ( ) and Loadlabel ()

4. Activityinfo class inherits from Packageiteminfo class

Description: Get information about <activity/> or <receiver/> nodes in the application. We can use it to get any properties we set, including theme, Launchmode, Launchmode, and other common methods that inherit from LoadIcon () and Loadlabel () in the Packageiteminfo class

5. ServiceInfo class inherits from Packageiteminfo class

Description: Similar to Activityinfo, represents <service> node information

6, Resolveinfo class

Description: Gets information about the previous level of the directory based on the <intent> node, usually <activity>, <receiver>, <service> node information. Common methods are LoadIcon (Packagemanager pm) and Loadlabel (Packagemanager pm)

Third, the example explains:

1, through the Packagemanager queryintentactivities method, query the system to meet Action_main and category_launcher applications, get their program name, package name, the entry class name. (Limited level, the ListView did not learn, can not do a simple launcher, but the principle of starting the application is mentioned in the previous article, interested can go to see: Android essay in the--activity to launch another application)

Mainactivity.java

 1 package Com.example.packagemanager; 2 3 Import java.util.Collections; 4 Import java.util.List; 5 6 Import android.app.Activity; 7 Import android.content.Intent; 8 Import Android.content.pm.PackageManager; 9 Import android.content.pm.resolveinfo;10 Import android.os.bundle;11 public class Mainactivity extends Activity {13 1          4 @Override15 protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 17         Setcontentview (R.layout.activity_main); Getappinfo ();}20 private void Getappinfo () {22 Get Packagemanager object Packagemanager pm = This.getpackagemanager (); 24//Settings <intent-filter> tags Conditions to be met in Intent Intent = new Intent (intent.action_main, null); Intent.addcategory (Intent.category_defa ULT); 27 28//Get ResolveInfo objects by queryintentactivities list<resolveinfo> Resolveinfos = Pm.queryinte Ntactivities (intent,30 PACKAGEMANAGER.MATCH_default_only); 31 32/Call system sort, sort by name 33//This sort is important, otherwise only system apps can be displayed, third-party applications cannot be displayed 34//Actually I test found there is no actually the same, is         The order of output is chaotic Collections.sort (resolveinfos,36 new Resolveinfo.displaynamecomparator (PM)); 37 38 for (ResolveInfo Resolveinfo:resolveinfos) {appName String = Resolveinfo.loadlabel (PM). toString ();//Received Take the application name PackageName string = resolveinfo.activityinfo.packagename;//package name in a string className = Resol                     veinfo.activityinfo.name;//Entry class name System.out.println ("program name:" + appName + "package Name:" + packageName43 + "Entry class Name:" + className); 44}45}46 47}

Output Result:

2, through the Packagemanager queryinstalledapplications method, filtering out the application of the system, third-party applications, installed on the sdcard.

Mainactivity.java

  1 package Com.example.packagemanager;  2 3 Import java.util.Collections;  4 Import java.util.List;  5 6 Import android.app.Activity;  7 Import Android.content.pm.ApplicationInfo;  8 Import Android.content.pm.PackageManager; 9 Import Android.os.Bundle; Ten import Android.view.View; Import Android.view.View.OnClickListener; public class Mainactivity extends Activity implements Onclicklistener {All public static final int filter_a Ll_app = 0; All applications public static final int filter_system_app = 1; System Program: public static final int filter_third_app = 2; Third-party applications public static final int filter_sdcard_app = 3; Install the application in SDcard private packagemanager pm; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancest ATE); Setcontentview (R.layout.activity_main); Findviewbyid (R.id.btn_all) Setonclicklistener (this); Findviewbyid (R.id.btn_system). SetOnclicklistener (this); Findviewbyid (R.id.btn_third). Setonclicklistener (this); Findviewbyid (R.id.btn_sdcard) Setonclicklistener (this);         30} 31 32/** 33 * Filter, select is System application, third-party application or sdcard application * */private void Filterapp (int type) {36 Get Packagemanager Object PNS = Getpackagemanager (); 38//Query installed applications list<applicationinfo> Applicationinfos = pm. Getinstalledapp Lications (packagemanager.get_uninstalled_packages); 41//Sort Collections.sort (Applicationinfos, New Applicationinfo.displaynamecomparato R (PM)); (type) {filter_all_app://all applications (ApplicationInfo ApplicationInfo : Applicationinfos) {getappinfo (applicationinfo);       Wuyi Case filter_system_app://System application for (ApplicationInfo Applicationinfo:applicationinfos) {53          if ((Applicationinfo.flags & applicationinfo.flag_system)! = 0) {getappinfo (applicat Ioninfo); The third-party application of the filter_third_app://(ApplicationInfo AP Plicationinfo:applicationinfos) {60//non-system application if (Applicationinfo.flags & Applic                 Ationinfo.flag_system) <= 0) {getappinfo (applicationinfo); 63} 64 System is applied, but after the update it becomes not the system applied the Applicationinfo.flags else if ((T & Applicationinfo.flag_updated_system_app )! = 0) {getappinfo (applicationinfo); _sdcard_app://sdcard Application (ApplicationInfo Applicationinfo:applicationinfos) {                 Pplicationinfo.flags = = Applicationinfo.flag_system) {getappinfo (applicationinfo); 73      } 74       } default:76 break; 77} 78} 79 80/** 81 * Get Application Information * * * Getappinfo private void (ApplicationInfo Applicatio Ninfo) {AppName string = Applicationinfo.loadlabel (PM). toString ();//Application name: String PackageName = appli cationinfo.packagename;//Package Name System.out.println ("Application name:" + appName + "package Name:" + PackageName); (arg0) {Arg0.getid ()) {* * * * * () . id.btn_all:93 System.out.println ("Output all application information: \ n"); 94 Filterapp (Filter_all_app); a break; r.id.btn_system:97 System.out.println ("Output system Application information: \ n"); 98 Filterapp (Filter_system_app); break;100 Case r.id.btn_third:101 System.out.println ("Output third-party application information: \ n"); 102 fi Lterapp (Filter_third_app); 103 break;104 Case r.id.btn_sdcard:105 SYSTEM.OUt.println ("Output sdcard application information: \ n"); 106 Filterapp (Filter_sdcard_app); 107 break;108 109 Default:1 Ten break;111}112}113 114}

Activity_main.xml

 <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http/ Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:orien tation= "vertical" > <button android:id= "@+id/btn_all" android:layout_width= "Match_parent" an        droid:layout_height= "Wrap_content" android:text= "all Apps"/> <button android:id= "@+id/btn_system" Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "System Application"/> < ; Button android:id= "@+id/btn_third" android:layout_width= "match_parent" android:layout_height= "Wrap_co Ntent "android:text=" third-party app "/> <button android:id=" @+id/btn_sdcard "Android:layout_width=" Ma Tch_parent "android:layout_height=" wrap_content "android:text=" SDcard application "/></LINEARLAYOUT> 

"Turn" Android essay--packagemanager detailed

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.