20

背景

根据 Android M 上的一项新功能(链接此处),您的应用程序之外的应用程序可以为其其中一个活动提供直接共享意图,例如,允许聊天应用程序将内容共享给确切的联系人,因此您同时选择聊天应用程序和联系人(一步而不是 2)。这可以在此图像上显示:

在此处输入图像描述

或者,至少我是这么理解的。

问题

关于这个新功能,我有 2 个问题:

  1. 在描述中,他们只显示了要放入清单中的内容,并提到使用“ChooserTargetService”。为了提供文本和图像应该做什么?

  2. 我想知道如何做相反的事情:如何查询所有这些“直接共享”项目(图像、文本和意图)并能够在自定义对话框中显示它们?

    我想这样做是因为我自己有一个自定义对话框,它允许选择要分享的内容和方式,而不仅仅是通过哪个应用程序。

4

2 回答 2

17

问题 1

在描述中,他们只显示了要放入清单中的内容,并提到使用“ChooserTargetService”。为了提供文本和图像应该做什么?

从扩展开始ChooserTargetService。您需要返回 a ListofChooserTarget以及如何创建这些目标完全取决于您。

public class YourChooserTargetService extends ChooserTargetService {

    @Override
    public List<ChooserTarget> onGetChooserTargets(ComponentName targetActivityName, IntentFilter matchedFilter) {
        final List<ChooserTarget> targets = new ArrayList<>();
        for (int i = 0; i < length; i++) {
            // The title of the target
            final String title = ...
            // The icon to represent the target
            final Icon icon = ...
            // Ranking score for this target between 0.0f and 1.0f
            final float score = ...
            // PendingIntent to fill in and send if the user chooses this target
            final PendingIntent action = ...
            targets.add(new ChooserTarget(title, icon, score, action));
        }
        return targets;
    }

}

AndroidManifest

现在你需要在你的中声明你ChooserTargetServiceAndroidManifest并做两件事:

  1. 绑定Service使用android.permission.BIND_CHOOSER_TARGET_SERVICE权限
  2. IntentFilterandroid.service.chooser.ChooserTargetService动作中包含一个

例如:

<service
    android:name=".YourChooserTargetService"
    android:label="@string/yourLabel"
    android:permission="android.permission.BIND_CHOOSER_TARGET_SERVICE">
    <intent-filter>
        <action android:name="android.service.chooser.ChooserTargetService" />
    </intent-filter>
</service>

Activity要处理的 中Intent,您需要添加meta-data标签android.service.chooser.chooser_target_service。例如:

<activity android:name=".YourShareActivity">

    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>

    <meta-data
        android:name="android.service.chooser.chooser_target_service"
        android:value=".YourChooserTargetService" />
</activity>

从这里开始,如果用户选择您的应用程序,则主要是调用Intent.createChooser然后处理数据的问题。

final Intent target = new Intent(Intent.ACTION_SEND);
target.setType("text/plain");
target.putExtra(Intent.EXTRA_TITLE, "Your title");
target.putExtra(Intent.EXTRA_TEXT, "Your text");
startActivity(Intent.createChooser(target, "ChooserTargetService Example"));

结果

结果

注意事项

每个排名分数用于ChooserTarget对目标进行排序,但仅在 UI 决定使用它时使用。按照ChooserTarget.getScore

在对来自多个源的目标进行排序和合并时,显示目标的 UI可能会考虑此分数

此外,据我所知,此功能实际上尚未在 Android MNC 预览版中实现。ChooserActivity包含一个它TODO

TODO:按排名分数保持排序

创建一个 newandroid.graphics.drawable.Icon时,您需要使用其中一个static初始化程序。

Icon.createWithBitmap();
Icon.createWithContentUri()
Icon.createWithData()
Icon.createWithFilePath()
Icon.createWithResource()

问题2

我想知道如何做相反的事情:如何查询所有这些“直接共享”项目(图像、文本和意图)并能够在自定义对话框中显示它们?

提供给的数据ChooserTargetService.onGetChooserTargets是动态的。因此,据我所知,没有直接的方法可以访问这些项目。

于 2015-06-09T00:11:35.093 回答
0

我对这个未来有不同的理解。

直到现在,当用户想要分享一些东西时,他们被要求选择他们想要分享的应用程序,然后这个应用程序处理了分享。

现在,用户将不再选择应用程序,而是从将处理共享的应用程序中选择内容。每个这样的选项都封装在android.service.chooser.ChooserTargetService.

因此,正如您在图像上看到的,它显示了 的一些产品ChooserTargetService,用户看到一些联系人 ui 还没有午餐或分享。

我相信您的对话可以以同样的方式触发。

于 2015-05-29T21:45:14.440 回答