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

Source: Internet
Author: User

Application Scenarios :

1. Display the version number of the application in the interface;
2, the user launches the application, the background to determine whether the application is the latest version.
Each of these scenarios requires that the app's version number be automatically obtained in the program.

Idea Introduction :
In Android, The version number of the application is configured in the Androidmanifest.xml file, and the PackageInfo class encapsulates all the information obtained from the configuration file, describing the overall information of the package content, so you can use the Versionname property of the PackageInfo object to get the application's The version number.
How do you get PackageInfo objects? can be obtained by Packagemanager objects. Packagemanager is a class that retrieves various information about an application package that is currently installed on the device. The Getpackageinfo method in the Packagemanager object can get the PackageInfo object, which needs to pass two parameters: the application package name and the condition. Typically, the application's package name can be obtained through the Getpackagename () method of the activity or context (activity inherits from the context), and the addition can have many settings, typically set to 0.
Finally, the Packagemanager object is fetched, and the context object provides the Getpackagemanager () method to get the object.

In summary, the template code is as follows: (Note that the method encapsulated here is in an activity, so use this instead of the context object)

1/** 2  * Get version number 3  * @return The current app version number 4  */5 public String GetVersion () {6     try {7         Packagemanager mana GER = This.getpackagemanager (); 8         PackageInfo info = manager.getpackageinfo (this.getpackagename (), 0); 9         String Version = info.versionname;10< C8/>return this.getstring (r.string.version_name) + version;11     } catch (Exception e) {         e.printstacktrace ();         return this.getstring (r.string.can_not_find_version_name);     }15}

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.