Obtain locally installed applications on Android

Source: Internet
Author: User

Enter the above keywords + android on Google, and you can find the code:

List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);

 

Although some code claims to be able to filter out the system's own applications, as long as you look at the code, you will find that the Boolean variable in it does not play a role in filtering.

Method 1: filter the packagename of Android. content. PM. packageinfo through the obtained installation package (including the installed and system applications,

However, Android and Google packages are easier to filter out, but they need to be adapted to different vendors;

Method 2: enumerate the \ data \ app folder. The *. APK file under it is the software installed on the local machine, and the *. APK file under \ System \ app is the software that comes with the system,

Because normal programs cannot read these two folders, the root permission is required, so this method does not work (using file. listfiles () + android. permission. factory_test permission still cannot enumerate files)

Although the folder "\ data \ app \" cannot be enumerated, we can access all the files under the (read) folder "\ data \ app, therefore, you can use the following method to obtain software installed on the local machine (excluding system applications ):

PackageManager pckMan = getPackageManager();List<PackageInfo> packs = pckMan.getInstalledPackages(0);count = packs.size();String name;int installedNum = 0;for(int i = 0; i < count; i++) {                 PackageInfo p = packs.get(i); if (p.versionName == null) continue; 
// Determine whether the software package is in the/data/APP directory file F1 = new file ("/data/APP/" + P. packagename + ". APK"); If (! F1.exists () continue; installednum ++;/*** Application name */name = P. applicationinfo. loadlabel (pckman). tostring ();}

Method 3: You can use UID to filter the installation packages of the system, because the uid in the Android system ranges from 1000 ~ 9999 is reserved for the system program, so you only need to judge the UID> 10000 of the package to determine that the program is a non-system program.

If (P. applicationinfo. uid> 10000) // non-system program

 

UID is allocated by the system during installation.

Method 4: This method should be optimal.

Applicationinfo appinfo = P. applicationinfo;/*** value for {@ link # flags}: If set, this application is installed in the * device's system image. */If (appinfo. flags & applicationinfo. flag_system)> 0) // system program else // not a system program

 

------ IPhone ------------

1. I recently studied the iPhone and found it quite troublesome to achieve this function on the iPhone:

Http://www.cocoachina.com/bbs/simple? T86829.html

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.