简体   繁体   中英

List of installed applications by package

This is my problem: I can get the list of the installed applications in my device, but i need to have a list with only determinate apps in list. I mean, for example, i would show in list only my apps.. How can i do? Can i just look at the package name? Is there any other way to filter this list to show the applications i want? This is the code i'm using:

The adapter:

public class LocalAppListAdapter extends ArrayAdapter<ApplicationInfo> {
    private List<ApplicationInfo> appsList = null;
    private Context context;
    private PackageManager packageManager;

    public LocalAppListAdapter(Context context, int textViewResourceId, List<ApplicationInfo> appsList) {
        super(context, textViewResourceId, appsList);
        this.context = context;
        this.appsList = appsList;
        packageManager = context.getPackageManager();
    }

    @Override
    public int getCount() {
        return ((null != appsList) ? appsList.size() : 0);
    }

    @Override
    public ApplicationInfo getItem(int position) {
        return ((null != appsList) ? appsList.get(position) : null);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (null == view) {
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(R.layout.local_app_list_row, null);
        }

        ApplicationInfo applicationInfo = appsList.get(position);
        if (null != applicationInfo) {
            TextView appName = (TextView) view.findViewById(R.id.app_name);
            TextView packageName = (TextView) view.findViewById(R.id.app_paackage);
            ImageView iconview = (ImageView) view.findViewById(R.id.app_icon);

            appName.setText(applicationInfo.loadLabel(packageManager));
            packageName.setText(applicationInfo.packageName);
            iconview.setImageDrawable(applicationInfo.loadIcon(packageManager));
        }
        return view;
    }
};

And the class i use to get the list:

public class LocalAppList extends AppCompatActivity {
    private PackageManager packageManager = null;
    private List<ApplicationInfo> applist = null;
    private LocalAppListAdapter listadaptor = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.local_app_list);

        packageManager = getPackageManager();

        new LoadApplications().execute();
    }



    private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
        ArrayList<ApplicationInfo> applist = new ArrayList<ApplicationInfo>();
        for (ApplicationInfo info : list) {
            try {
                if (null != packageManager.getLaunchIntentForPackage(info.packageName)) {
                    applist.add(info);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return applist;
    }

    private class LoadApplications extends AsyncTask<Void, Void, Void> {
        private ProgressDialog progress = null;

        @Override
        protected Void doInBackground(Void... params) {
            applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
            listadaptor = new LocalAppListAdapter(LocalAppList.this, R.layout.local_app_list, applist);

            return null;
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
        }

        @Override
        protected void onPostExecute(Void result) {
            ListView list = (ListView) findViewById(R.id.localAppList);
            list.setAdapter(listadaptor);
            progress.dismiss();
            super.onPostExecute(result);
        }

        @Override
        protected void onPreExecute() {
            progress = ProgressDialog.show(LocalAppList.this, null, "Loading application info...");
            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }
    }
}

So this is actually the full list with all applications.. I need find a way to filter the list showing for example only the google apps or which i want to show.

You can use this method to display list of installed application in which it filters all the system level application ie the application which can not be launched.

/**
 * This method is used to fetch all the application installed in device.
 * @param Context
 * @return List<ApplicationInfo>
 */
public static List<ApplicationInfo> getInstalledApplication(Context c) 
{
    List<ApplicationInfo> installedApps = c.getPackageManager().getInstalledApplications(PackageManager.PERMISSION_GRANTED);
    List<ApplicationInfo> launchableInstalledApps = new ArrayList<ApplicationInfo>();
    for(int i =0; i<installedApps.size(); i++)
    {
        if(c.getPackageManager().getLaunchIntentForPackage(installedApps.get(i).packageName) != null)
        {
            //If you're here, then this is a launch-able app
            launchableInstalledApps.add(installedApps.get(i));
        }
    }
    return launchableInstalledApps;
}

I hope this will help.

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