使用Category.LAUNCHER获取所有已安装的应用程序

时间:2013-06-01 18:38:07

标签: java android

我的问题:
我希望使用Intent Category Category.LAUNCHER获取所有已安装的应用程序。 所以我想获得将在启动器中显示的所有应用程序 然后解决包裹和名称 例如package: "com.android.phone" -- name: "Phone"

我的代码基本上有效,但我想,不,我知道这可以变得更容易:

final PackageManager pm = getPackageManager();
Intent main = new Intent(Intent.ACTION_MAIN, null);
main.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> packages = pm.queryIntentActivities(main, 0);

for (ResolveInfo resolve_info : packages)
{
    try
    {
        //THIS TWO
        String package_name = resolve_info.activityInfo.packageName;
        String app_name = (String)pm.getApplicationLabel(pm.getApplicationInfo(package_name, PackageManager.GET_META_DATA));

        Log.i("TEST", "package = <" + package_name + "> name = <" + app_name + ">");
    }
    catch(Exception e)
    {
        //package not found -- should never happen
    }
}

那么更好/更快或更容易的方式呢?

2 个答案:

答案 0 :(得分:1)

final PackageManager pm = getActivity().getPackageManager();

final IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);
filter.addCategory(Intent.CATEGORY_LAUNCHER);

List<IntentFilter> outFilters = new ArrayList<IntentFilter>();
outFilters.add(filter);

List<ComponentName> outActivities = new ArrayList<ComponentName>();
pm.getPreferredActivities(outFilters, outActivities, null);

答案 1 :(得分:0)

    Intent startupIntent = new Intent(Intent.ACTION_MAIN);
    startupIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    PackageManager pm =  this.getPackageManager();
    List<ResolveInfo> activities = pm.queryIntentActivities(startupIntent, 0);
    Log.i(TAG, "Found " + activities.size() + " activities.");

MainActivity中的扩展AppCompatActivity或Activity的