I have a custom listPreference I would like to display a list of apps that can be launched (contain an activity with CATEGORY_LAUNCHER). The selection will be used later to launch the application. When I did a search for the solution, the list also contained apps 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);
}
}
Solved:
    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;
    };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With