知道它的名字,获取一个活动的ResolveInfo对象?

时间:2013-03-19 22:43:06

标签: java android

我想将我的活动(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);
            }
        }

2 个答案:

答案 0 :(得分:6)

只需将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);

答案 1 :(得分:2)

如果您不仅知道应用程序的packageName,那么另一个答案是:

Intent intent = new Intent();
intent.setPackage(packageName);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ResolveInfo result = packageManager.resolveActivity(intent, 0);