代码之家  ›  专栏  ›  技术社区  ›  Kevin Gaudin

如何从Activity.StartActivity(意向)选择器中排除我自己的活动?

  •  11
  • Kevin Gaudin  · 技术社区  · 14 年前

    我的应用程序适用于图片。它可以将多张图片作为输入,进行处理,然后再次发送到另一个应用程序。

    因此,我的主要 Activity 已在上声明意向筛选器 ACTION_SEND_MULTIPLE 对于 image/* mimetypes并可能导致发布新的 Intent 使用相同的操作和数据类型 Activity.startActivity(Intent) .

    有没有一种方法可以将我自己的活动从显示给用户的应用程序列表中排除 startActivity() 打电话?

    2 回复  |  直到 6 年前
        1
  •  7
  •   CommonsWare    14 年前

    不是直接的,阿法克。但是,您可以使用 PackageManager queryIntentActivityOptions() 它允许过滤掉你自己(或其他东西)。

        2
  •  0
  •   Minas Mina    6 年前

    你需要做的是:

    查询可用的应用程序以处理意图。

    • 如果有一个不属于你的应用程序的默认活动来处理它,直接打开它(使用 startActivity() )
    • 否则,显示一个意向选择器,不包括您自己的应用程序包。因此,即使您的应用程序被设置为处理这种意图的默认应用程序,选择器仍然会显示,这是您想要的。

    Here's a blog post 详细解释一下。

    或者直接复制粘贴代码:

    
    /**
     * Attempts to start an activity to handle the given intent, excluding activities of this app.
     * <ul>
     *     <li>If the user has set a default activity (which does not belong in this app's package), it is opened without prompt.</li>
     *     <li>Otherwise, an intent chooser is displayed that excludes activities of this app's package.</li>
     * </ul>
     *
     * @param context context
     * @param intent intent to open
     * @param chooserTitle the title that will be displayed for the intent chooser in case one needs to be displayed.
     */
    static void startActivityExcludingOwnApp(@NonNull Context context, @NonNull Intent intent, @NonNull String chooserTitle) {
    
        PackageManager packageManager = context.getPackageManager();
        List<Intent> possibleIntents = new ArrayList<>();
    
        Set<String> possiblePackageNames = new HashSet<>();
        for (ResolveInfo resolveInfo : packageManager.queryIntentActivities(intent, 0)) {
    
            String packageName = resolveInfo.activityInfo.packageName;
            if (!packageName.equals(context.getPackageName())) {
    
                Intent possibleIntent = new Intent(intent);
                possibleIntent.setPackage(resolveInfo.activityInfo.packageName);
                possiblePackageNames.add(resolveInfo.activityInfo.packageName);
    
                possibleIntents.add(possibleIntent);
            }
        }
    
        @Nullable ResolveInfo defaultResolveInfo = packageManager.resolveActivity(intent, 0);
    
        if (defaultResolveInfo == null || possiblePackageNames.isEmpty()) {
            throw new ActivityNotFoundException();
        }
    
        // If there is a default app to handle the intent (which is not this app), use it.
        if (possiblePackageNames.contains(defaultResolveInfo.activityInfo.packageName)) {
            context.startActivity(intent);
        }
        else { // Otherwise, let the user choose.
            Intent intentChooser = Intent.createChooser(possibleIntents.remove(0), chooserTitle);
            intentChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, possibleIntents.toArray(new Parcelable[]{}));
            context.startActivity(intentChooser);
        }
    }