本文主要介绍android 6.0新特性中的直接分享功能。所谓直接分享(Direct Share)就是用户可以直接分享相关内容,不需要先打开第二个应用,再选择分享目标去分享内容,可以直接向分享目标分享内容。
看图就清楚了,如下图,
上面红色圈起来的就是直接分享目标,下面绿色圈起来的是可分享的应用。比如我们向一个聊天软件里的联系人分享内容的时候,选择聊天软件和某个联系人的操作是在一步完成的,而不是两步。
具体实现:
1、继承ChooserTargetService服务,实现里面的
List onGetChooserTargets(x,x),
返回直接分享目标的集合。
public class SampleChooserTargetService extends ChooserTargetService {
@Override
public List<ChooserTarget> onGetChooserTargets(ComponentName targetActivityName, IntentFilter matchedFilter) {
final List<ChooserTarget> targets = new ArrayList<>();
for (int i = 0; i < length; i++) {
final String targetName = ...
final Icon targetIcon = ...
final float targetRanking = ...
final ComponentName targetComponentName = ...
final Bundle targetExtras = ...
targets.add(new ChooserTarget(
targetName, targetIcon, targetRanking, targetComponentName, targetExtras
));
}
return targets;
}
}
创建什么样的Target完全取决于你自己。
public ChooserTarget(CharSequence title, Icon icon, float score,
ComponentName componentName, @Nullable Bundle intentExtras)
每个ChooserTarget都有5个属性,依次是
title:target的标题;
icon:target的图标;
score:取值在0.0~1.0之间,这个值的作用相当于用来排序,如果有很多Targets,score排在后面的话可能不会被显示出来;
componentName:如果选择某个Target时要启动的组件名称(比如分享的Activity);
intentExtras:Bundle of extras to merge with the extras of the launched intent,意思就是把intentExtras合并到要启动的组件的intent中去。
2、在manifest声明该service,如下
<service android:name=".MyChooserTargetService"
android:label="@string/app_name"
android:permission="android.permission.BIND_CHOOSER_TARGET_SERVICE">
<intent-filter>
<action android:name="android.service.chooser.ChooserTargetService"/>
</intent-filter>
</service>
这里注意添加 BIND_CHOOSER_TARGET_SERVICE permission和ChooserTargetService action。
3、在manifest声明分享界面,比如
<activity android:name=".ShareActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<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=".MyChooserTargetService"/>
</activity>
在ShareActivity处理分享的内容,这里必须添加meta-data,name为android.service.chooser.chooser_target_service,value为第一步实现的MyChooserTargetService。
4、发起分享
比如,
public void shareText(String text) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TITLE,text);
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.setType("text/plain");
startActivity(Intent.createChooser(shareIntent, "分享到"));
}
它的原理是系统在响应SEND的Intent时,会检测符合要求的Activity是否暴露给ChooserTargetService,如果暴露了则把ChooserTargetService方法onGetChooserTargets返回的ChooserTarget显示给用户,否则就不显示,只显示可分享的应用。
这样我们就实现了为一个应用添加直接分享的功能。
参考链接1:http://stackoverflow.com/questions/30518321/on-android-m-how-to-configure-the-direct-share-capabilities-image-text-an
参考链接2:https://github.com/bboyfeiyu/android-tech-frontier/blob/master/issue-32/%E5%AE%9E%E7%8E%B0%E5%AE%89%E5%8D%936.0%E7%9A%84%E7%9B%B4%E6%8E%A5%E5%88%86%E4%BA%AB%EF%BC%88Direct%20Share%20%EF%BC%89%E5%8A%9F%E8%83%BD.md