按包安装的应用程序列表

时间:2016-04-27 15:23:13

标签: java android listview android-package-managers

这是我的问题:我可以在我的设备中获取已安装应用程序的列表,但我需要列表中只有确定应用程序的列表。我的意思是,例如,我只会在列表中显示我的应用程序..我该怎么办?我可以看一下包名吗?有没有其他方法可以过滤此列表以显示我想要的应用程序?这是我使用的代码:

适配器:

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;
    }
};

我用来获取列表的类:

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);
        }
    }
}

所以这实际上是包含所有应用程序的完整列表..我需要找到一种方法来过滤列表,例如只显示谷歌应用程序或我想要显示的列表。

1 个答案:

答案 0 :(得分:0)

您可以使用此方法显示已安装应用程序的列表,其中筛选所有系统级应用程序,即无法启动的应用程序。

/**
 * 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;
}

我希望这会有所帮助。