如何对从ApplicationInfo检索到的已安装应用程序进行排序

时间:2015-08-14 14:12:22

标签: sorting installed-applications

我想按字母顺序对已安装的应用列表进行排序

我的活动的xml文件只包含一个列表视图。

以下是我展示应用的活动:

public class AllAppsActivity extends ListActivity {
private PackageManager packageManager = null;
private List<ApplicationInfo> applist = null;
private ApplicationAdapter listadaptor = null;

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

    packageManager = getPackageManager();

    new LoadApplications().execute();
}



private void displayAboutDialog() {
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("about title");
    builder.setMessage("about_desc");

    builder.setPositiveButton("Know More", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://javatechig.com"));
            startActivity(browserIntent);
            dialog.cancel();
        }
    });
    builder.setNegativeButton("No Thanks!", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });

    builder.show();
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    ApplicationInfo app = applist.get(position);
    try {
        Intent intent = packageManager
                .getLaunchIntentForPackage(app.packageName);

        if (null != intent) {
            startActivity(intent);
        }
    } catch (ActivityNotFoundException e) {
        Toast.makeText(AllAppsActivity.this, e.getMessage(),
                Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        Toast.makeText(AllAppsActivity.this, e.getMessage(),
                Toast.LENGTH_LONG).show();
    }
}

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 ApplicationAdapter(AllAppsActivity.this,
                R.layout.snippet_list_row, applist);

        return null;
    }

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

    @Override
    protected void onPostExecute(Void result) {
        setListAdapter(listadaptor);
        progress.dismiss();
        super.onPostExecute(result);
    }

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

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

}

这是我的应用适配器:

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

TextView appName;
TextView packageName;
ImageView iconview;


public ApplicationAdapter(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.snippet_list_row, null);
    }

    ApplicationInfo applicationInfo = appsList.get(position);
    if (null != applicationInfo) {

        appName = (TextView) view.findViewById(R.id.app_name);
        packageName = (TextView) view.findViewById(R.id.app_paackage);
        iconview = (ImageView) view.findViewById(R.id.app_icon);

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

}

实际上,我不知道如何按应用名称对其进行排序。有什么想法吗?

0 个答案:

没有答案