我如何获得安装应用程序名称列表

时间:2013-06-16 14:18:32

标签: android

这是我的源代码,它使用包名显示所有安装应用的列表,但我想使用应用名称显示列表中的所有应用。我怎么做?假设我想在列表视图中显示名称为“viber”或“whatsapp”的应用程序。 下面是我的代码,它只显示我提供的软件包名称的应用程序,但我只想显示我提供的名称的应用程序,而不是软件包名称。

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_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(MainActivity.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);


           if ( ((p.packageName).equals("com.gfsmart.filemanager")) || 
       ((p.packageName).equals("com.mypackage")) || 
  ((p.packageName).equals("com.doodlejoy.studio.kidsdoojoy")) || 
  ((p.packageName).equals("com.fullfat.android.agentdash"))) {
    //           if ( (p.applicationInfo).equals("com.mypackage")) {

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

    }

1 个答案:

答案 0 :(得分:1)

final PackageManager pm = getPackageManager();
//get a list of installed apps.
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

然后将它们添加到ListArray&lt;串GT;使用这个for循环

for (ApplicationInfo packageInfo : packages) 
    myList.Add(packageInfo.packageName);

然后添加整个ListArray&lt;串GT;到列表视图

 ListAdapter la = new ArrayAdapter<String>(this, R.layout.simplerow,
 AppNames);
 myListView.setAdapter(la);

在onItemClickListener中,您可以使用

获取Intent
packages.get(ClickedRowNumber).packageName;
祝你好运