揭秘Android 6.0 之 Direct Share(直接分享)
前言:Android M提供了新的API,支持快速分享。你可以在应用中定义可以启动特殊Activity的“直接分享目标”(direct share targets ),这些直接分享目标通过分享按钮暴露给用户,这个特性允许用户直接将内容分享给其他应用中的某个人或者某个群组
1、项目演示 下面通过一个Demo程序演示如何进行直接分享。写一个简单的Activity,分享“Hello”给朋友 当点击“Share”按钮时弹出“直接分享目标”,“Messenger”,”DirecShare”是可通过它们进行分享的应用(就像6.0之前点击分享弹出的可分享的应用列表),“Messenger”,”DirecShare”上面区域即是直接分享目标
点击一个直接分享目标”Chang”,弹出分享界面
点击SEND,完成分享
2、项目实现 2.1 继承ChooserTargetService 要开启直接分享你必须先定义一个Class继承ChooserTargetService,在Manifest中声明这个服务,在该声明中,指定BIND_CHOOSER_TARGET_SERVICE 权限,并且指定Intent Filter Action为SERVICE_INTERFACE。 下面的例子展示了如何在Manifest中声明ChooserTargetService: - <service
- android:name=".SampleChooserTargetService"
- 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>
复制代码 重写ChooserTargetService的方法onGetChooserTargets() - @Override
- public List<ChooserTarget> onGetChooserTargets(ComponentName targetActivityName,
- IntentFilter matchedFilter) {
- ComponentName componentName = new ComponentName(getPackageName(),
- SendMessageActivity.class.getCanonicalName());
- // The list of Direct Share items. The system will show the items the way they are sorted
- // in this list.
- ArrayList<ChooserTarget> targets = new ArrayList<>();
- for (int i = 0; i < Contact.CONTACTS.length; ++i) {
- Contact contact = Contact.byId(i);
- Bundle extras = new Bundle();
- extras.putInt(Contact.ID, i);
- targets.add(new ChooserTarget(
- // The name of this target.
- contact.getName(),
- // The icon to represent this target.
- Icon.createWithResource(this, contact.getIcon()),
- // The ranking score for this target (0.0-1.0); the system will omit items with
- // low scores when there are too many Direct Share items.
- 0.5f,
- // The name of the component to be launched if this target is chosen.
- componentName,
- // The extra values here will be merged into the Intent when this target is
- // chosen.
- extras));
- }
- return targets;
- }
复制代码 该方法返回的List中存放的ChooserTarget就是上面看到的“直接分享对象”,看下面关键代码
- targets.add(new ChooserTarget(
- // The name of this target.
- contact.getName(),
- // The icon to represent this target.
- Icon.createWithResource(this, contact.getIcon()),
- // The ranking score for this target (0.0-1.0); the system will omit items with
- // low scores when there are too many Direct Share items.
- 0.5f,
- // The name of the component to be launched if this target is chosen.
- componentName,
- // The extra values here will be merged into the Intent when this target is
- // chosen.
- extras));
复制代码 创建ChooserTarget对象时定义了直接分享对象的name,icon,extras,还有componentName,这里componentName就是当选择该直接分享对象时要启动的component 在该Demo中,就是分享界面的Activity。
- ComponentName componentName = new ComponentName(getPackageName(),
- SendMessageActivity.class.getCanonicalName());
复制代码 看到的界面也就是
2.2 将Activity暴露给ChooserTargetService 只完成ChooserTargetService还不行,还必须将接收Action androd.intent.action.SEND的Activity暴露给ChooserTargetService才可以,通过<meta-data>标签 - 1, <activity
- android:name=".SendMessageActivity"
- android:label="@string/app_name"
- android:theme="@style/DirectShareDialogTheme">
- <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=".SampleChooserTargetService" />
- </activity>
复制代码 它的原理是系统在响应SEND的Intent时,会检测符合要求的Activity是否暴露给ChooserTargetService,如果暴露了则把ChooserTargetService方法onGetChooserTargets返回的ChooserTarget显示给用户,否则就不显示,只显示可分享的应用。
|