CATEGORY_ALTERNATIVE的确切目的是什么

问题描述 投票:0回答:1

我一直在阅读CATEGORY_ALTERNATIVE上的文档,我想知道“对用户当前查看的数据的替代操作”的确切含义是什么。

有人可以提供更多有关如何使用此类别的示例吗?

我只找到了有关创建Menus的一些信息,并且还引用了“当系统向用户提供选择工作的活动时,系统会列出具有此类别的过滤器的活动”。但是,在第二种情况下,再说一遍-如果我们需要包含CATEGORY_DEFAULT以便活动接收隐式意图,这将如何工作。

android android-intent categories intentfilter
1个回答
0
投票

[假设您正在使用某些Messenger应用,并且朋友向您发送了一个有趣的链接。然后,您可以使用它做几件事:

  • 在浏览器中打开
  • 与他人共享
  • 也许将其保存在某些笔记应用中
  • ...

如果您将链接作为“您当前正在查看的数据”,那么,如果Messenger向您提供了一个选项,可以从您设备上当前可用的一系列替代操作中选择适当的操作,那就太好了。

使用Intent.CATEGORY_ALTERNATIVEMenu.addIntentOptions(),该框架为应用程序提供了一种方式,该应用程序是某种类型数据的初始“所有者”,可为用户提供进一步操作的访问权限。这些动作可以由设备上运行的任何应用程序执行,这些应用程序可以通过Manifest.xml中的条目(更具体地说是<intent-filter>标签中的条目)宣布其处理此特定数据类型的能力。

让我们考虑一个非常简单-如果不是很现实-例如:

我的应用具有显示给用户的String值。替代动作可以是

  • 基于某些正则表达式解析String
  • String的字母进行排序
  • [转换String,例如大写字母
  • ...

我的应用程序将其ActivityFragment覆盖onCreateOptionsMenu()如下,(Fragment的代码段)

@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.home_menu, menu);

    // Create an Intent that describes the requirements to fulfill, to be included
    // in our menu. The offering app must include a category value of Intent.CATEGORY_ALTERNATIVE.
    Intent intent = new Intent();
    intent.putExtra(Intent.EXTRA_TEXT, "Lorem ipsum");
    intent.setType("text/plain");
    intent.addCategory(Intent.CATEGORY_ALTERNATIVE);

    // Search and populate the menu with acceptable offering applications.
    menu.addIntentOptions(
            R.id.intent_group,  // Menu group to which new items will be added
            0,      // Unique item ID (none)
            0,      // Order for the items (none)
            this.getActivity().getComponentName(),   // The current activity name
            null,   // Specific items to place first (none)
            intent, // Intent created above that describes our requirements
            0,      // Additional flags to control items (none)
            null);  // Array of MenuItems that correlate to specific items (none)
}

能够处理给定ActivityString将在IntentFilter中将Manifest.xml声明为其标签的子标签:

<activity android:name=".ToUpperCaseActivity">
      <intent-filter android:label="@string/label_action_text_to_uppercase">
            <action android:name="de.ddvsidedown.alternativeoptionsapp.action.ALL_CAPS" />
            <category android:name="android.intent.category.ALTERNATIVE" />
            <data android:mimeType="text/plain" />
        </intent-filter>
</activity>

<activity android:name=".SortActivity">
        <intent-filter android:label="@string/label_action_sort_text">
            <action android:name="de.ddvsidedown.alternativeoptionsapp.action.SORT" />
            <category android:name="android.intent.category.ALTERNATIVE" />
            <data android:mimeType="text/plain" />
        </intent-filter>
</activity>

然后,当用户点击溢出图标时,我的应用程序将显示以下弹出菜单...

popup menu with "Text to upper case" and "Sort text"

...,运行时将直接导航到所选的Activity。在那里,可以按以下方式检索文本:

 if(getIntent().hasExtra(Intent.EXTRA_TEXT)) {

        char[] ca = getIntent().getCharSequenceExtra(Intent.EXTRA_TEXT).toString().toCharArray();
        Arrays.sort(ca);
        String message = buildString(ca);
        Log.d(TAG, "onCreate: " + message);
        TextView textView = findViewById(R.id.tvResult);
        textView.setText(message);
}

[请注意,检索数据的确切代码取决于数据类型,就像完全没有Intent标签或在<category>中具有<category android:name="android.intent.category.DEFAULT" />的“普通” <intent-filter>一样。

如果我们需要包含CATEGORY_DEFAULT以使活动接收隐式意图,这将如何工作

如果要使其显示在选择器对话框中,则需要<category android:name="android.intent.category.DEFAULT"/><intent-filter>中的Activity。顺便说一句,Activity可以有多个<intent-filter>标签

让我们修改示例:我的应用程序还具有“ SHARE” Button。如果单击,将执行以下代码:

 /**
 * Will only work with <category android:name="android.intent.category.DEFAULT" />
 */
private void shareText() {
    // Create the text message with a string
    Intent sendIntent = new Intent();
    //sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, "Lorem ipsum");
    sendIntent.setType("text/plain");

    // Verify that the intent will resolve to an activity
    if (sendIntent.resolveActivity(getActivity().getPackageManager()) != null) {
        startActivity(sendIntent);
    }
}

运行时将向用户显示选择器对话框。每个带有Activity<intent-filter><category android:name="android.intent.category.DEFAULT"/>都将显示应用程序标题和应用程序的启动器图标。 (虽然在同一应用中可以同时包含两个ActivityCATEGORY_DEFAULT,但显然这看起来并不好-可以对文本和/或图标进行调整,但这绝对超出了范围这个问题)

如果我注释掉ACTION_SEND,那么选择器对话框的标题为“使用完成操作”,而如果我设置为ACTION_SEND,则标题为“与...共享”。

系统如何决定[处理]具有“替代”类别的许多应用程序,尤其是在不知道操作名称的情况下进行>]

我的应用通过设置(或省略)操作,类别以及数据类型来配置其Intent。运行时始终选择与我的Activity的所有要求匹配的Intent。这意味着,另一个Activity可能会提供处理数据类型“文本/ *”的功能,而不仅仅是“文本/纯文本”,但是如果它仅提供“图像/ *”,那么它将不会显示给用户。

© www.soinside.com 2019 - 2024. All rights reserved.