在AndroidM上,如何配置"direct-share"的能力(图片,文字),如何查询物品?
On Android M, how to configure the "direct-share" capabilities (image, text), and how to query the items?
背景
根据 Android M (link here) 上的一项新功能,您的应用程序外部的应用程序可以提供对其活动之一的直接共享意图,允许,例如,一个聊天应用程序将内容分享给一个确切的联系人,所以你同时选择聊天应用程序和联系人(一步而不是 2)。这可以显示在这张图片上:
或者,至少我是这么理解的。
问题
关于这个新功能我有 2 个问题:
在描述中,他们只显示了要放入清单中的内容,并提到使用“ChooserTargetService”。提供文字和图片应该怎么做?
我想知道如何做相反的事情:如何查询所有这些“直接共享”项目(图像、文本和意图)并能够在自定义对话框?
我想这样做是因为我自己有一个自定义对话框,它允许选择分享的内容和方式,而不仅仅是通过哪个应用程序。
我对这个未来有不同的理解
直到现在,当用户想要分享一些东西时,他们被要求选择他们想要分享的应用程序,然后这个应用程序处理分享。
现在,用户不再选择应用程序,而是从处理共享的应用程序中选择内容。每个这样的选项都封装在 android.service.chooser.ChooserTargetService
.
中
因此,正如您在图片上看到的,它显示了 ChooserTargetService
的一些产品,用户看到一些联系人 ui 还没有午餐或共享。
我相信你的对话可以用同样的方式触发。
问题 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
。您将需要 return List
of 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;
}
}
Android清单
现在您需要在 AndroidManifest
中声明您的 ChooserTargetService
并做两件事:
- 使用
android.permission.BIND_CHOOSER_TARGET_SERVICE
权限绑定 Service
- 在
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>
在要处理 Intent
的 Activity
中,您需要添加 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
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()
问题二
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 M (link here) 上的一项新功能,您的应用程序外部的应用程序可以提供对其活动之一的直接共享意图,允许,例如,一个聊天应用程序将内容分享给一个确切的联系人,所以你同时选择聊天应用程序和联系人(一步而不是 2)。这可以显示在这张图片上:
或者,至少我是这么理解的。
问题
关于这个新功能我有 2 个问题:
在描述中,他们只显示了要放入清单中的内容,并提到使用“ChooserTargetService”。提供文字和图片应该怎么做?
我想知道如何做相反的事情:如何查询所有这些“直接共享”项目(图像、文本和意图)并能够在自定义对话框?
我想这样做是因为我自己有一个自定义对话框,它允许选择分享的内容和方式,而不仅仅是通过哪个应用程序。
我对这个未来有不同的理解
直到现在,当用户想要分享一些东西时,他们被要求选择他们想要分享的应用程序,然后这个应用程序处理分享。
现在,用户不再选择应用程序,而是从处理共享的应用程序中选择内容。每个这样的选项都封装在 android.service.chooser.ChooserTargetService
.
因此,正如您在图片上看到的,它显示了 ChooserTargetService
的一些产品,用户看到一些联系人 ui 还没有午餐或共享。
我相信你的对话可以用同样的方式触发。
问题 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
。您将需要 return List
of 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;
}
}
Android清单
现在您需要在 AndroidManifest
中声明您的 ChooserTargetService
并做两件事:
- 使用
android.permission.BIND_CHOOSER_TARGET_SERVICE
权限绑定Service
- 在
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>
在要处理 Intent
的 Activity
中,您需要添加 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
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()
问题二
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
的数据是动态的。因此,据我所知,没有直接的方法来访问这些项目。