这是我的代码,显示所有安装应用程序

时间:2013-06-13 10:29:55

标签: android

这是我的源代码,下面显示了所有已安装的系统应用程序。我想只显示选定的应用程序,只有5个应用程序的名称,我发誓不显示所有应用程序我该怎么办?请帮帮我

       public class ListInstalledApps extends Activity implements OnItemClickListener {

 /* whether or not to include system apps */
 private static final boolean INCLUDE_SYSTEM_APPS = false;

 private ListView mAppsList;
 private AppListAdapter mAdapter;
 private List<App> mApps;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  mAppsList = (ListView) findViewById(R.id.appslist);
  mAppsList.setOnItemClickListener(this);

  mApps = loadInstalledApps(INCLUDE_SYSTEM_APPS);

  mAdapter = new AppListAdapter(getApplicationContext());
  mAdapter.setListItems(mApps);
  mAppsList.setAdapter(mAdapter);

  new LoadIconsTask().execute(mApps.toArray(new App[]{}));
 }

  @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

  final App app = (App) parent.getItemAtPosition(position);

  AlertDialog.Builder builder = new AlertDialog.Builder(this);

  String msg = app.getTitle() + "\n\n" + 
     "Version " + app.getVersionName() + " (" +
     app.getVersionCode() + ")" +
     (app.getDescription() != null ? ("\n\n" + app.getDescription()) : "");

  builder.setMessage(msg)
  .setCancelable(true)
  .setTitle(app.getTitle())
  .setIcon(mAdapter.getIcons().get(app.getPackageName()))
  .setPositiveButton("Launch", new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int id) {
        // start the app by invoking its launch intent
        Intent i = getPackageManager().getLaunchIntentForPackage(app.getPackageName());
        try {
           if (i != null) {
              startActivity(i);
           } else {
              i = new Intent(app.getPackageName());
              startActivity(i);
           }
        } catch (ActivityNotFoundException err) {
           Toast.makeText(ListInstalledApps.this, "Error launching app", 
  Toast.LENGTH_SHORT).show();
        }
     }
  })
  .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int id) {
        dialog.cancel();
     }
  });
  AlertDialog dialog = builder.create();
  dialog.show();
}

 /**
  * Uses the package manager to query for all currently installed apps which are put  
 into beans and returned
 * in form of a list.
 * 
 * @param includeSysApps whether or not to include system applications
 * @return a list containing an {@code App} bean for each installed application 
 */
 private List<App> loadInstalledApps(boolean includeSysApps) {
   List<App> apps = new ArrayList<App>();

  // the package manager contains the information about all installed apps
  PackageManager packageManager = getPackageManager();

  List<PackageInfo> packs = packageManager.getInstalledPackages(0); 
 //PackageManager.GET_META_DATA 

  for(int i=0; i < packs.size(); i++) {
     PackageInfo p = packs.get(i);
     ApplicationInfo a = p.applicationInfo;
     // skip system apps if they shall not be included
     if ((!includeSysApps) && ((a.flags & ApplicationInfo.FLAG_SYSTEM) == 1)) {
        continue;
     }
     App app = new App();
     app.setTitle(p.applicationInfo.loadLabel(packageManager).toString());
     app.setPackageName(p.packageName);
     app.setVersionName(p.versionName);
     app.setVersionCode(p.versionCode);
     CharSequence description = p.applicationInfo.loadDescription(packageManager);
     app.setDescription(description != null ? description.toString() : "");
     apps.add(app);
  }
  return apps;
  }

 /**
 * An asynchronous task to load the icons of the installed applications.
 */
  private class LoadIconsTask extends AsyncTask<App, Void, Void> {
  @Override
  protected Void doInBackground(App... apps) {

     Map<String, Drawable> icons = new HashMap<String, Drawable>();
     PackageManager manager = getApplicationContext().getPackageManager();

     for (App app : apps) {
        String pkgName = app.getPackageName();
        Drawable ico = null;
        try {
           Intent i = manager.getLaunchIntentForPackage(pkgName);
           if (i != null) {
              ico = manager.getActivityIcon(i);
           }
        } catch (NameNotFoundException e) {
           Log.e("ERROR", "Unable to find icon for package '" + pkgName + "': " + 
 e.getMessage());
        }
        icons.put(app.getPackageName(), ico);
     }
     mAdapter.setIcons(icons);

     return null;
  }

  @Override
  protected void onPostExecute(Void result) {
     mAdapter.notifyDataSetChanged();
  }
 }

 }

2 个答案:

答案 0 :(得分:0)

试试这个 -

提供要显示的应用程序的package_name 并将其与设备

中包含的包名称进行比较

//为浏览器应用程序提供包名为com.android.browser

            for (int i = 0; i < packs.size(); i++) {
       if ((p.packageName).equals("com.android.browser")) {
            App app = new App();
          app.setTitle(p.applicationInfo.loadLabel(packageManager).toString());
        app.setPackageName(p.packageName);
        app.setVersionName(p.versionName);
        app.setVersionCode(p.versionCode);
       CharSequence description = p.applicationInfo.loadDescription(packageManager);
       app.setDescription(description != null ? description.toString() : "");
       apps.add(app);
  }
}

希望这会对你有所帮助。

答案 1 :(得分:0)

您需要一个筛选过程,以查看应用程序是否在您提到的应用程序列表中,将您输入的应用程序包名称存储在数组中,并查看当前应用程序是否在该列表中(如果有)将其添加到列表中然后使用此列表显示listView

private List<App> loadInstalledApps(boolean includeSysApps) {
   List<App> apps = new ArrayList<App>();

  // the package manager contains the information about all installed apps
  PackageManager packageManager = getPackageManager();

  List<PackageInfo> packs = packageManager.getInstalledPackages(0); 
 //PackageManager.GET_META_DATA 

  for(int i=0; i < packs.size(); i++) {
     PackageInfo p = packs.get(i);
     ApplicationInfo a = p.applicationInfo;
     // skip system apps if they shall not be included
     if ((!includeSysApps) && ((a.flags & ApplicationInfo.FLAG_SYSTEM) == 1)) {
        continue;
     }
     App app = new App();
     app.setTitle(p.applicationInfo.loadLabel(packageManager).toString());
     app.setPackageName(p.packageName);
     app.setVersionName(p.versionName);
     app.setVersionCode(p.versionCode);
     CharSequence description = p.applicationInfo.loadDescription(packageManager);
     app.setDescription(description != null ? description.toString() : "");

if( Arrays.asList(appliation1.packageName,appliation2.packageName,appliation3.packageName).contains(p.packageName) )  
     apps.add(app);
  }
  return apps;
  }