How to use the Intent class to show the user's apps

1

I want my app to show the user's applications in ListView .

I have tried to investigate and the mention of this class came to me in English the problem is that it did not work out how to use it for that purpose.

Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);

If you know please help.

    
asked by PowerApp 21.10.2016 в 00:30
source

1 answer

3

By means of an Intent defined as CATEGORY_LAUNCHER , you can obtain a list of installed application packages:

Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List listaPaquetesAppsInstaladas = getPackageManager().queryIntentActivities(intent, 0)

To add the data in a ListView , this is a complete example:

    //Crea List para almacenar packagename de las aplicaciones.
    List<String> listaPaquetesAppsInstaladas = new ArrayList<>();
    Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);

    //Obtiene datos de apps instaladas
    List<ResolveInfo> listAppsInstaladas = getPackageManager().queryIntentActivities(intent, 0);
    for(ResolveInfo info : listAppsInstaladas) {
        // be added
        ApplicationInfo applicationInfo;
        if (info == null || (applicationInfo = info.activityInfo.applicationInfo) == null
                || !applicationInfo.enabled || listaPaquetesAppsInstaladas.contains(applicationInfo.packageName)) {
            continue;
        }
        listaPaquetesAppsInstaladas.add(applicationInfo.packageName);
    }

    //Instancía adapter con datos obtenidos
    final ArrayAdapter adapter = new ArrayAdapter(this,
            android.R.layout.simple_list_item_1, listaPaquetesAppsInstaladas);

    //Muestra datos en ListView
    ListView listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(adapter);

To obtain the application packages in ListView .

    
answered by 21.10.2016 в 01:09