4

我想将我的活动 (com.myapp.launcher.settings) 添加到一个空的 ArrayList。

    ArrayList<ResolveInfo> selectedApps = new ArrayList<ResolveInfo>();
    selectedApps.add(/*WHAT GOES IN HERE?*/);

但我不知道如何通过我的活动获取 ResolveInfo 对象。


我设法制定了一种解决方法,循环遍历所有应用程序以查找我的活动。但它不是非常有效或实用:

    // Get an array list of all apps
    ArrayList<ResolveInfo> allApps = (ArrayList<ResolveInfo>) pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
    for(final ResolveInfo app : allApps) {
        // If it's name is "com.myapp.launcher.settings" add it
        if(app.activityInfo.name.equals("com.myapp.launcher.settings")) {
                selectedApps.add(app);
            }
        }
4

2 回答 2

9

只需resolveActivity与明确的Intent. 大概是这样的:

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.myapp", "com.myapp.launcher.settings"));
ResolveInfo app = pm.resolveActivity(intent, 0);
selectedApps.add(app);
于 2013-03-19T22:59:37.147 回答
3

如果您不只知道packageName应用程序的另一个答案:

Intent intent = new Intent();
intent.setPackage(packageName);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ResolveInfo result = packageManager.resolveActivity(intent, 0);
于 2014-06-18T07:44:56.610 回答