Get a list of installed applications that you can run

I have a custom listPreference that I would like to display a list of applications that can be launched (contain activity with CATEGORY_LAUNCHER). The selection will be used later to launch the application. When I searched for a solution, the list also contained applications that could not be launched. Is there any way to narrow this down?

public class AppSelectorPreference extends ListPreference { @Override public int findIndexOfValue(String value) { return 0; //return super.findIndexOfValue(value); } public AppSelectorPreference(Context context, AttributeSet attrs) { super(context,attrs); PackageManager pm = context.getPackageManager(); List<PackageInfo> appListInfo = pm.getInstalledPackages(0); CharSequence[] entries = new CharSequence[appListInfo.size()]; CharSequence[] entryValues = new CharSequence[appListInfo.size()]; try { int i = 0; for (PackageInfo p : appListInfo) { if (p.applicationInfo.uid > 10000) { entries[i] = p.applicationInfo.loadLabel(pm).toString(); entryValues[i] = p.applicationInfo.packageName.toString(); i++; } } } catch (Exception e) { e.printStackTrace(); } setEntries(entries); setEntryValues(entryValues); } 

}

+4
source share
2 answers

It is decided:

  final Context context = getBaseContext(); final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); final List<ResolveInfo> pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0); CharSequence[] entries = new CharSequence[pkgAppsList.size()]; CharSequence[] entryValues = new CharSequence[pkgAppsList.size()]; int i = 0; for ( ResolveInfo P : pkgAppsList ) { entryValues[i] = (CharSequence) P.getClass().getName(); entries[i] = P.loadLabel(context.getPackageManager()); ++i; }; 
+5
source

@ Frazerm63 I think you are missing this thing in your code.

  Intent localIntent = new Intent("android.intent.action.MAIN", null); localIntent.addCategory("android.intent.category.LAUNCHER"); List localList = localPackageManager.queryIntentActivities(localIntent, 0); Collections.sort(localList, new ResolveInfo.DisplayNameComparator(localPackageManager)); 

you need to pass your PackageManager object in the above code .means this localPackageManager

I donโ€™t really understand how you can use this in user code, but it will help you understand how to filter only any category application.

+4
source

Source: https://habr.com/ru/post/1383316/


All Articles