2016-04-27 3 views
0

Das ist mein Problem: Ich kann die Liste der installierten Anwendungen auf meinem Gerät abrufen, aber ich brauche eine Liste mit nur bestimmten Apps in der Liste. Ich meine, zum Beispiel würde ich in der Liste nur meine Apps zeigen. Wie kann ich tun? Kann ich nur den Paketnamen sehen? Gibt es eine andere Möglichkeit, diese Liste zu filtern, um die Anwendungen anzuzeigen, die ich möchte? Dies ist der Code ich verwende:Liste der installierten Anwendungen nach Paket

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

Und die Klasse i verwenden, um die Liste zu erhalten:

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

dies So ist eigentlich die vollständige Liste mit allen Anwendungen .. Ich muss einen Weg finden, die Liste zu filtern, die zum Beispiel nur die Google Apps zeigt oder die ich zeigen möchte.

+1

Implementieren Sie Ihren Filter in 'checkForLaunchIntent()' und fügen Sie nur Elemente zu Ihrer Ergebnisliste hinzu, wenn diese Ihren Kriterien entsprechen. – CommonsWare

+0

Eigentlich war das eine meiner Ideen, aber ich konnte keinen Ausweg finden. Hast du eine Idee? Kannst du mir zeigen, welche Implementierungen ich machen muss? –

+0

Nein, weil ich nicht weiß, was "zum Beispiel nur die Google Apps oder welche ich zeigen möchte" bedeutet. Verwenden Sie im Allgemeinen "ApplicationInfo" und "PackageManager", um festzustellen, ob diese App in der Liste enthalten ist. – CommonsWare

Antwort

0

Sie können diese Methode verwenden, um eine Liste der installierten Anwendungen anzuzeigen, in der die gesamte Anwendung auf Systemebene, d. H. Die Anwendung, die nicht gestartet werden kann, gefiltert wird.

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

Ich hoffe, dass dies helfen wird.