获取可以启动的已安装应用程序列表

问题描述:

我有一个自定义listPreference我想显示可以启动的应用程序列表(包含CATEGORY_LAUNCHER的活动)。该选择将在稍后用于启动应用程序。当我搜索解决方案时,该列表还包含无法启动的应用程序。有什么办法可以缩小这个范围吗?获取可以启动的已安装应用程序列表

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

}

+0

可能从可能回答会帮助你得到这样的应用程序包名称的排序列表里面可以是包含具有CATEGORY_LAUNCHER和android.intent.action.MAIN活动。 – Herry

解决:

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

@ Frazerm63我认为你缺少这个东西在你的代码

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

你必须通过你的PackageManager对象在上面的代码中.means这localPackageManager

我不太了解如何在用户代码中使用它,但这将有助于让您想法仅过滤某些类别应用程序。

+0

查看一段代码,这会给我一个列表。我将如何将其改为ListPreference的数组? – Frazerm63

+0

纠错:它不应该转换为数组,但CharSequence – Frazerm63

+0

@ Frazerm63所以你得到解决它..或保持... – Herry