简体   繁体   中英

Android/Java PackageManager list only specific system apps

I have a list that is populated by the package manager and it dieplays a list of all system apps. I need to find a way to "filter" that list so only specific apps show. I have a list of apps that I will "allow" the user to manimpulate. Is that possible to show ONLY those apps out of all system apps? I mean filter for their name and if found on device' then show on the list? Thank you so much for any advice! All is appreciated! This is the relevant code that populates the list (the parent is fragment and the list is being created in async task).

private class GearAppsToDebloat extends AsyncTask<Void, Void, Void> {
    private ProgressDialog progress = null;
    //Using package manager to get all installed apps on the device
    final List<PackageInfo> packageList = packageManager
            .getInstalledPackages(PackageManager.GET_META_DATA);
    final List<PackageInfo> packageList1 = packageManager
            .getInstalledPackages(0);


    @Override
    protected Void doInBackground(Void... params) {


        try {
            packageList1.clear();
            for (int n = 0; n < packageList.size(); n++) {

                PackageInfo PackInfo = packageList.get(n);
                // List only system apps
                if (((PackInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM
                    ) != 0) == true)


                {
                    try {

                        packageList1.add(packageList.get(n));
                        // Sort App list on name basis
                        Collections.sort(packageList1, new Comparator<PackageInfo>()
                        {
                            public int compare(PackageInfo o1, PackageInfo o2) {
                                // Return sorted list of packages
                                return o1.applicationInfo.loadLabel(getActivity().getPackageManager()).toString()
                                        .compareToIgnoreCase(o2.applicationInfo.loadLabel(getActivity().getPackageManager())
                                                .toString());
                            }
                        });


                    } catch (NullPointerException e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }





        return null;
    }

Sorry, I just did this in C# in Xamarin but I'm sure you can write the equivalent.

I got the needed applications from using a lambda expression on the GetInstalledApplications method. It selects the application if the ClassName is present in the list of apps I am looking for.

This also provided me with a very quick return, using other methods I've come across took about 8 seconds to do the same task.

const string Facebook = "com.facebook.katana.app.FacebookApplication";
const string Email = "com.android.email.Email";
const string Whatsapp = "com.whatsapp.App";
const string Twitter = "com.twitter.android.TwitterApplication";
private IEnumerable<string> AvailableApps;

private void SearchAvailableApps()
        {
            List<string> apps = new List<string>() { Facebook, Email, Whatsapp, Twitter };
            PackageManager pm = this.Activity.PackageManager;
            AvailableApps = pm.GetInstalledApplications(0).Where(x => apps.Contains(x.ClassName)).Select(x => x.ClassName);            
        }

You should now have the list of apps that you know the user has installed on his/her device.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM