android - 在 Android M 上,如何配置 "direct-share"能力(图像、文本),以及如何查询项目?

标签 android android-intent android-manifest android-6.0-marshmallow

背景

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

enter image description here

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

问题

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

  1. 在描述中,他们只显示了要放入 list 中的内容,并提到使用“ChooserTargetService”。提供文字和图片需要做什么?

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

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

最佳答案

问题 1

In the description, they only show what to put in the manifest, and they mention using "ChooserTargetService". What should be done in order to provide the texts and images?

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

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

现在您需要在 AndroidManifest 中声明您的 ChooserTargetService 并做两件事:

  1. 使用android.permission.BIND_CHOOSER_TARGET_SERVICE权限绑定(bind)Service
  2. android.service.chooser.ChooserTargetService 操作中包含一个 IntentFilter

例如:

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

在要处理 IntentActivity 中,您需要添加 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"));

结果

results

注意事项

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

The UI displaying the target may take this score into account when sorting and merging targets from multiple sources

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

TODO: Maintain sort by ranking scores

创建新的 android.graphics.drawable.Icon 时,您需要使用 static 初始化程序之一。

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

问题 2

I'd like to know how to do the opposite : how can I query all of those "direct-share" items (images, texts, and intents) and be able to show them on a customized dialog?

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

关于android - 在 Android M 上,如何配置 "direct-share"能力(图像、文本),以及如何查询项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30518321/

相关文章:

android - 检查列表中每个项目的空值

android - 我可以在一个页面上放置两个 AdMob 广告(固定横幅广告和可滚动原生广告)

Android 共享 Intent EXTRA_STREAM

android - 使用 Intent 将对象从一个 Activity 传递到另一个 Activity 有什么问题

android - 广播接收器 ACTION_SEND 未显示

java - 为什么这种在按钮单击方法上播放声音不起作用

java - 为什么不发送电子邮件?

java - 如何从 Android 中的另一个应用程序启动 Activity

android - 在 Android 应用程序中添加多个文件提供程序

安卓工作室错误 : Execution failed for task ':app:processDebugManifest'