Android app-PackageManager details

Source: Internet
Author: User

Android app-PackageManager details
1. Functions of PackageManager: 1. Install and uninstall an Application. 2. query permission information. 3. query application information (Application, activity, Referer, service, provider, and related attributes) 4. query installed applications 5. add and delete permission 6. Clear user data, cache, code segments, and other related classes and methods of PackageManager. 1. PackageManager copy Code Description: obtain information about the installed application. You can use the getPackageManager () method. Common method: public abstract PackageManager getPackageManager () function: obtain a PackageManger object public abstract Drawable getApplicationIcon (String packageName) parameter: packageName package name function: returns the icon of the given package name, otherwise, return the null public abstract ApplicationInfo getApplicationInfo (String packageName, int flags) parameter: packagename package name flags the ApplicationInfo is the flags. Generally, you can directly assign the constant 0 to the function: returns the public abstract List of the ApplicationInfo object <ApplicationInfo> getInstall EdApplications (int flags) parameter: flag is generally GET_UNINSTALLED_PACKAGES, then all ApplicationInfo is returned. We can filter flags of ApplicationInfo to obtain what we need. Function: returns all PackageInfo public abstract List <PackageInfo> getInstalledPackages (int flags) parameters of a given condition. The preceding function: returns all PackageInfo public abstract ResolveInfo resolveActivity (Intent intent, int flags) of a given condition) parameter: intent search condition. The action and category flags: MATCH_DEFAULT_ONLY: Category must contain the CATEGORY_DEFAULT Activity to match GET_INTENT_FILTERS: match the Intent condition. GET_RESOLVED_FILTER: matches the Intent condition. function: returns the ResolveInfo object for the given condition (essentially Activi Ty) public abstract List <ResolveInfo> queryIntentActivities (Intent intent, int flags) parameter same as above function: return all ResolveInfo objects of the given condition (essentially Activity ), set object public abstract ResolveInfo resolveService (Intent intent, int flags) parameter same as above function: Return ResolveInfo object (essentially Service) public abstract List <ResolveInfo> queryIntentServices (Intent intent Intent, int flags) parameter same as above function: return all ResolveInfo objects (essentially Service) for the given condition, copy the code of the collection object 2. PackageItemInfo class description: The base classes of all nodes in the ndroidManifest. xml file provide basic information about these nodes: label, icon, and meta-data. Instead of directly using it, it inherits from the subclass and then calls the corresponding method. 3. The ApplicationInfo class inherits from the PackageItemInfo class copy Code Description: obtains information about the <application> node in a specific reference program. Field Description: flags field: FLAG_SYSTEM System Application FLAG_EXTERNAL_STORAGE indicates that the common methods used by the application to install in sdcard inherit from loadIcon () and loadLabel () in the PackageItemInfo class () copy Code 4. The ActivityInfo class inherits from the PackageItemInfo class. Description: obtains information about the <activity/> or <worker ER/> nodes in the application. We can use it to obtain any attributes we set, including theme, launchMode, launchmode, and other common methods inherited to loadIcon () and loadLabel () in the PackageItemInfo class () 5. ServiceInfo class inherited from PackageItemInfo class description: similar to ActivityInfo, it indicates <service> node information 6. ResolveInfo class description: obtains the information of the previous directory based on the <intent> node, usually <activity>, <worker ER>, and <service> node information. Common methods include loadIcon (PackageManager pm) and loadLabel (PackageManager pm). III. Example Description: 1. query all the applications that meet the requirements of ACTION_MAIN and CATEGORY_LAUNCHER through the queryIntentActivities method of PackageManager, obtain their program name, package name, and entry class name. (The level is limited. ListView is not good at learning and cannot be used as a simple starter. However, the principle of starting an application is mentioned in the previous article. If you are interested, you can check it out: android casually -- start another application in Activity) MainActivity. java copy code 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 12 public cl Ass MainActivity extends Activity {13 14 @ Override15 protected void onCreate (Bundle savedInstanceState) {16 super. onCreate (savedInstanceState); 17 setContentView (R. layout. activity_main); 18 getAppInfo (); 19} 20 21 private void getAppInfo () {22 // get PackageManager object 23 PackageManager pm = this. getPackageManager (); 24 // set the condition 25 intent Intent intent = new Intent (Intent. ACTION_MAIN, null); 2 6 intent. addCategory (Intent. CATEGORY_DEFAULT); 27 28 // get ResolveInfo object 29 List through queryIntentActivities <ResolveInfo> resolveInfos = pm. queryIntentActivities (intent, 30 PackageManager. MATCH_DEFAULT_ONLY); 31 32 // call system sorting. sort by name 33 // This sorting is very important; otherwise, only system applications can be displayed, third-party applications cannot be displayed. 34 // In Fact, I tested whether the output order is actually the same, that is, 35 Collections in disorder. sort (resolveInfos, 36 new ResolveInfo. displayNameComparator (pm); 37 38 for (ResolveInfo resolveIn Fo: resolveInfos) {39 String appName = resolveInfo. loadLabel (pm ). toString (); // obtain the application name 40 String packageName = resolveInfo. activityInfo. packageName; // package name 41 String className = resolveInfo. activityInfo. name; // entry class name 42 System. out. println ("program name:" + appName + "package name:" + packageName43 + "entry class name:" + className); 44} 45} 46 47} copy the code output result: 2. Use the queryInstalledApplications method of PackageManager to filter out system applications, third-party applications, and applications installed on SDCard.. MainActivity. java copy code 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; 10 import android. view. view; 11 import android. view. view. onClickListener; 12 13 public class MainActivity extends Activity impleme CNT OnClickListener {14 15 public static final int FILTER_ALL_APP = 0; // all applications 16 public static final int FILTER_SYSTEM_APP = 1; // system program 17 public static final int FILTER_THIRD_APP = 2; // third-party application 18 public static final int FILTER_SDCARD_APP = 3; // application installed on SDCard 19 private PackageManager pm; 20 21 @ Override 22 protected void onCreate (Bundle savedInstanceState) {23 super. onCreate (savedInstanc EState); 24 setContentView (R. layout. activity_main); 25 26 findViewById (R. id. btn_all ). setOnClickListener (this); 27 findViewById (R. id. btn_system ). setOnClickListener (this); 28 findViewById (R. id. btn_third ). setOnClickListener (this); 29 findViewById (R. id. btn_sdcard ). setOnClickListener (this); 30} 31 32/** 33 * filter, system application, third-party application, or SDCard application 34 */35 private void filterApp (int type) {36 // get PackageManager Object 37 pm = getPackageManager (); 38 // query the 39 lists of installed applications <ApplicationInfo> applicationInfos = pm 40. getInstalledApplications (PackageManager. GET_UNINSTALLED_PACKAGES); 41 // sort 42 Collections. sort (applicationInfos, 43 new ApplicationInfo. displayNameComparator (pm); 44 45 switch (type) {46 case FILTER_ALL_APP: // all applications 47 for (ApplicationInfo applicationInfo: applicationInfos) {48 getAppInfo (appli CationInfo); 49} 50 break; 51 case FILTER_SYSTEM_APP: // System Application 52 for (ApplicationInfo applicationInfo: applicationInfos) {53 if (applicationInfo. flags & ApplicationInfo. FLAG_SYSTEM )! = 0) {54 getAppInfo (applicationInfo); 55} 56} 57 case FILTER_THIRD_APP: // third-party application 58 59 for (ApplicationInfo applicationInfo: applicationInfos) {60 // non-System Application 61 if (applicationInfo. flags & ApplicationInfo. FLAG_SYSTEM) <= 0) {62 getAppInfo (applicationInfo); 63} 64 // system application, but after the update, it becomes 65 else if (applicationInfo. flags & ApplicationInfo. FLAG_UPDATED_SYSTEM_APP )! = 0) {66 getAppInfo (applicationInfo); 67} 68} 69 case FILTER_SDCARD_APP: // SDCard application 70 for (ApplicationInfo applicationInfo: applicationInfos) {71 if (applicationInfo. flags = ApplicationInfo. FLAG_SYSTEM) {72 getAppInfo (applicationInfo); 73} 74} 75 default: 76 break; 77} 78} 79 80/** 81 * obtain application information 82 */83 private void getAppInfo (ApplicationInfo applicationInfo) {84 String appName = applicationInfo. loadLabel (pm ). toString (); // Application name 85 String packageName = applicationInfo. packageName; // package name 86 System. out. println ("Application name:" + appName + "package name:" + packageName); 87} 88 89 @ Override 90 public void onClick (View arg0) {91 switch (arg0.getId () {92 case R. id. btn_all: 93 System. out. println ("output all application information: \ n"); 94 filterApp (FILTER_ALL_APP); 95 break; 96 case R. id. btn_system: 97 System. out. println ("Output System Application Information: \ n"), 98 filterApp (FILTER_SYSTEM_APP), 99 break; 100 case R. id. btn_third: 101 System. out. println ("output third-party application information: \ n"); 102 filterApp (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 110 default: break; 111} 112} 113 114} copy code activity_main.xml copy Code <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: orientation = "vertical"> <Button android: id = "@ + id/btn_all" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "All Applications"/> <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_content" android: text = "third-party application"/> <Button android: id = "@ + id/btn_sdcard" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "SDCard application"/> </LinearLayout>

Related Article

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.