在Android M上,如何配置"直接共享"功能(图像,文本)以及如何查询项目?

and*_*per 19 android android-manifest android-intent android-6.0-marshmallow

背景

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

在此输入图像描述

或者,至少这是我从中理解的.

这个问题

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

  1. 在描述中,它们仅显示清单中的内容,并提及使用"ChooserTargetService".为了提供文本和图像应该怎么做?

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

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

adn*_*eal 17

问题1

在描述中,它们仅显示清单中的内容,并提及使用"ChooserTargetService".为了提供文本和图像应该怎么做?

从扩展开始ChooserTargetService.你需要返回一个List,ChooserTarget你如何创建这些目标完全取决于你.

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

}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

从这里开始,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"));
Run Code Online (Sandbox Code Playgroud)

结果

结果

注意事项

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

当从多个源排序和合并目标时,显示目标的UI 可以考虑该分数

此外,据我所知,此功能尚未在Android MNC预览中实现.在ChooserActivity包含TODO了它:

TODO:通过排名得分来维持排序

创建新文件时android.graphics.drawable.Icon,您需要使用其中一个static初始化程序.

Icon.createWithBitmap();
Icon.createWithContentUri()
Icon.createWithData()
Icon.createWithFilePath()
Icon.createWithResource()
Run Code Online (Sandbox Code Playgroud)

问题2

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

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