"Android" gets the installed APK file information (PackageInfo, ResolveInfo) (app image, app name, package name, etc.) from your phone

Source: Internet
Author: User

As we all know, through the Packagemanager can obtain the mobile phone installed apk file information, the specific code is as follows

[Java]View Plaincopyprint?
    1. Packagemanager Packagemanager = This.getpackagemanager ();
    2. list<packageinfo> packageinfolist = packagemanager.getinstalledpackages (0);

Through the above method, you can get all the applications installed in the phone, including the manual installation of the APK package information, but also includes the system preinstalled application software information, to distinguish between the two types of software can use the following methods:

A. PackageInfo obtained from Packageinfolist, and then acquired ApplicationInfo through Packageinfo.applicationinfo.

B. The value of Judgment (Applicationinfo.flags & Applicationinfo.flag_system), which is greater than 0 o'clock, indicates that the acquired app is an app preinstalled by the system, and vice versa is a manually installed app.

You can look at the code and say it's written in the comments.

Get the app's code:

[Java]View Plaincopyprint?
  1. /**
  2. * Inquire about non-system applications in mobile phone
  3. * @param context
  4. * @return
  5. */
  6. public static list<packageinfo> Getallapps (context context) {
  7. List<packageinfo> apps = new arraylist<packageinfo> ();
  8. Packagemanager Pmanager = Context.getpackagemanager ();
  9. Get all apps in your phone
  10. list<packageinfo> paklist = pmanager.getinstalledpackages (0);
  11. for (int i = 0; i < paklist.size (); i++) {
  12. PackageInfo Pak = (packageinfo) paklist.get (i);
  13. Determine if a non-system preinstalled application
  14. if ((Pak.applicationInfo.flags & Pak.applicationInfo.FLAG_SYSTEM) <= 0) {
  15. Customs applications
  16. Apps.add (Pak);
  17. }
  18. }
  19. return apps;
  20. }

Get picture, App name, package name:

[Java]View Plaincopyprint?
  1. Packagemanager Pmanager = MessageSendActivity.this.getPackageManager ();
  2. list<packageinfo> applist = Utils.getallapps (messagesendactivity.this);
  3. for (int i=0;i<applist.size (); i++) {
  4. PackageInfo pinfo = Applist.get (i);
  5. Shareiteminfo Shareitem = new Shareiteminfo ();
  6. Set Icon
  7. Shareitem.seticon (Pmanager.getapplicationicon (pinfo.applicationinfo));
  8. Set Application Name
  9. Shareitem.setlabel (Pmanager.getapplicationlabel (pinfo.applicationinfo). toString ());
  10. Set Package Name
  11. Shareitem.setpackagename (Pinfo.applicationInfo.packageName);
  12. }

Where the Shareiteminfo class is my local custom, you can ignore!

Alternatively, someone might be looking for a list of sharing apps, and here's the next one.

Get the code for the app that supports sharing:

[Java]View Plaincopyprint?
  1. /**
  2. * Find all the apps that support sharing in your phone
  3. * @param context
  4. * @return
  5. */
  6. public static list<resolveinfo> Getshareapps (context context) {
  7. list<resolveinfo> Mapps = new arraylist<resolveinfo> ();
  8. Intent intent=new Intent (intent.action_send,null);
  9. Intent.addcategory (Intent.category_default);
  10. Intent.settype ("Text/plain");
  11. Packagemanager Pmanager = Context.getpackagemanager ();
  12. Mapps = Pmanager.queryintentactivities (Intent,packagemanager.component_enabled_state_default);
  13. return Mapps;
  14. }

Because of this method, the returned PackageInfo object is not. But ResolveInfo. So the way to get pictures, app names, and package names is different, as follows:

[Java]View Plaincopyprint?
  1. Packagemanager Pmanager = MessageSendActivity.this.getPackageManager ();
  2. /********************* Find all the apps that support sharing in your phone *********************/
  3. list<resolveinfo> resolvelist = Utils.getshareapps (messagesendactivity.this);
  4. for (int i=0;i<resolvelist.size (); i++) {
  5. ResolveInfo resolve = Resolvelist.get (i);
  6. Shareiteminfo Shareitem = new Shareiteminfo ();
  7. Set Icon
  8. Shareitem.seticon (Resolve.loadicon (Pmanager));
  9. Set Application Name
  10. Shareitem.setlabel (Resolve.loadlabel (Pmanager). toString ());
  11. Set Package Name
  12. Shareitem.setpackagename (Resolve.activityInfo.packageName);
  13. }

Summarize:

Get the specific information method by PackageInfo:

Package name Get method: Packageinfo.packagename

Icon Get Get Method: Packagemanager.getapplicationicon (ApplicationInfo)

Application name Get method: Packagemanager.getapplicationlabel (ApplicationInfo)

Access method using permissions: Packagemanager.getpackageinfo (packagename,packagemanager.get_permissions)

. requestedpermissions

Get the specific information method by ResolveInfo:

Package name Get method: Resolve.activityInfo.packageName

Icon Get Get Method: Resolve.loadicon (Packagemanager)

App Name Get method: Resolve.loadlabel (Packagemanager). ToString ()

"Android" gets the installed APK file information (PackageInfo, ResolveInfo) (app image, app name, package name, etc.) from your phone

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.