java - 获取 Activity 的 ResolveInfo 对象,知道它的名称?

标签 java android

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

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

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


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

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

最佳答案

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

关于java - 获取 Activity 的 ResolveInfo 对象,知道它的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15511940/

相关文章:

android - BadParcelableException : ClassNotFoundException when unmarshalling: (empty classname)

java - JMockit - 模拟第二次并忽略第一次

Android javax.annotation.processing 包丢失

java - 我该如何修复 javax.net.ssl.SSLHandshakeException : no cipher suites in common?

java - 如何计算我点击嵌套 for 循环网格内部的哪个 "cell"

android - 在我的 phonegap 应用程序中获得 iPhone 5 中的一些额外空白

Java/Android : Get Facebook User Info From Graph Api, 在TextView中显示

java - 使用 GridBagConstraints 更新布局

java - Jackson 将 Map 合并到 json 对象中

java - 在 ResourceSupport 中设置实体中不存在的表单字段的正确位置