空意图选择器(没有应用程序可以执行此操作)

时间:2013-06-28 11:30:13

标签: android android-intent

我的intentchooser是基于白名单(只有一些选择将出现在意图选择器中)。代码是基于另一个代码的代码;黑名单申请。我从here获得了该代码,this是与之相关的相关讨论。

选择器的创建方式:

        String[] whitelist = new String[] { "org.schiphol", "nl.negentwee", "org.schipholsecurity", "org.chineseschiphol", "nl.ns", "com.tomtom" };

    Intent intent = new Intent(Intent.ACTION_MAIN);
    //intent.addCategory(Intent.CATEGORY_LAUNCHER);

    startActivity(generateCustomChooserIntent(intent, whitelist));

我用来创建列入白名单的选择器的方法;

// Method:
private Intent generateCustomChooserIntent(Intent prototype, String[] whiteList) {
    List<Intent> targetedShareIntents = new ArrayList<Intent>();
    List<HashMap<String, String>> intentMetaInfo = new ArrayList<HashMap<String, String>>();
    Intent chooserIntent;

    Intent dummy = new Intent(prototype.getAction());
    dummy.setType(prototype.getType());
    List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(dummy, 0);
    MyLog.i(LOG_TAG, "Apps installed on device:" + resInfo.size());

    if (!resInfo.isEmpty()) {
        for (ResolveInfo resolveInfo : resInfo) {
            // MyLog.i(LOG_TAG, "Looking at:" + resolveInfo.activityInfo.packageName);

            if (resolveInfo.activityInfo == null) {
                MyLog.e(LOG_TAG, "resolved application has no activity info, so it is not usable.");
                continue;
            }

            if (Arrays.asList(whiteList).contains(resolveInfo.activityInfo.packageName)) {
                // MyLog.i(LOG_TAG, "=============================> accepted");

                HashMap<String, String> info = new HashMap<String, String>();
                info.put("packageName", resolveInfo.activityInfo.packageName);
                info.put("className", resolveInfo.activityInfo.name);
                info.put("simpleName", String.valueOf(resolveInfo.activityInfo.loadLabel(getPackageManager())));
                intentMetaInfo.add(info);
            } else {
                // MyLog.i(LOG_TAG, "rejected");
            }
        }

        if (!intentMetaInfo.isEmpty()) {
            MyLog.i(LOG_TAG, "--- done compiling list ---");

            // TODO enable sorting again
            // sorting for nice readability
            // Collections.sort(intentMetaInfo, new Comparator<HashMap<String, String>>() {
            // @Override
            // public int compare(HashMap<String, String> map, HashMap<String, String> map2) {
            // return map.get("simpleName").compareTo(map2.get("simpleName"));
            // }
            // });

            MyLog.i(LOG_TAG, "--- creating custom intent list ---");

            // create the custom intent list
            for (HashMap<String, String> metaInfo : intentMetaInfo) {
                MyLog.i(LOG_TAG, "adding " + metaInfo.get("packageName") + " to the intent list");

                Intent targetedShareIntent = (Intent) prototype.clone();
                targetedShareIntent.setPackage(metaInfo.get("packageName"));
                targetedShareIntent.setClassName(metaInfo.get("packageName"), metaInfo.get("className"));
                targetedShareIntents.add(targetedShareIntent);
            }

            MyLog.i(LOG_TAG, "--- done compiling intent list ---");
            MyLog.i(LOG_TAG, "total count targetedShareIntents: " + targetedShareIntents.size());

            chooserIntent = Intent.createChooser(targetedShareIntents.remove(targetedShareIntents.size() - 1), "Selecteer reis app (1)");
            MyLog.i(LOG_TAG, "--- chooser created ---");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[] {}));

            MyLog.e(LOG_TAG, "returning filled (custom) chooser");
            return chooserIntent;
        }
    }

    MyLog.e(LOG_TAG, "returning default chooser (empty)");
    return Intent.createChooser(prototype, "Selecteer reis app");
}

现在发生的事情是结果选择器显示“没有应用程序可以执行此操作”虽然logcat显示已选择5个应用程序。

Logcat记录结果:

06-28 13:04:48.679: I/NavigationTypeActivity(9400): Apps installed on device:356
06-28 13:04:48.687: I/NavigationTypeActivity(9400): --- done compiling list ---
06-28 13:04:48.687: I/NavigationTypeActivity(9400): --- creating custom intent list ---
06-28 13:04:48.687: I/NavigationTypeActivity(9400): adding org.chineseschiphol to the intent list
06-28 13:04:48.687: I/NavigationTypeActivity(9400): adding org.schiphol to the intent list
06-28 13:04:48.687: I/NavigationTypeActivity(9400): adding org.schipholsecurity to the intent list
06-28 13:04:48.687: I/NavigationTypeActivity(9400): adding nl.negentwee to the intent list
06-28 13:04:48.687: I/NavigationTypeActivity(9400): --- done compiling intent list ---
06-28 13:04:48.687: I/NavigationTypeActivity(9400): total count targetedShareIntents: 4
06-28 13:04:48.687: I/NavigationTypeActivity(9400): --- chooser created ---
06-28 13:04:48.687: E/NavigationTypeActivity(9400): returning filled (custom) chooser

1 个答案:

答案 0 :(得分:1)

我从Android大师那里得到了一些反馈,告诉我应该用一个工作意图定义选择器,例如playstore或gmail等。从理论上讲,您可以提供OWN应用程序的启动意图(您确定已安装自己的应用程序)。

Intent chooserIntent = Intent.createChooser(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=gmail")), "example");

之后,这就是列表从列表中删除单个项目的原因(您可能不希望自己的应用程序出现在选择器中)。