Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of installed applications that can be launched

Tags:

android

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

}

like image 848
frazer Avatar asked Jan 18 '23 04:01

frazer


1 Answers

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;
    };
like image 155
frazer Avatar answered Feb 03 '23 22:02

frazer