According to a new feature on Android M (link here), apps outside your app can offer to give a direct sharing intent to one of its activities, allowing, for example, a chatting app to share the content to an exact contact, so you choose both the chatting-app and the contact at the same time (one step instead of 2) . This can be shown on this image:
Or, at least that's what I've understood from it.
I have 2 questions regarding this new feature:
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?
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?
I want to do it because I have a customized dialog myself, that allows to choose what to share and how, and not just through which app.
Direct Share is a feature that allows apps to show app-specific options directly in the system Intent chooser dialog. Users can then jump directly into your app when sharing content from another app.
On a Samsung phone, you simply need to go to your “Advanced features” section and disable “Direct share.” On an LG phone, go to the Networks tab, Share & connect, and, finally, Sharing panel. If you don't have either of these devices, then read on.
Tap Manage apps & devices. In the “Overview” tab, next to “Share apps,” tap Send. Select which apps to share. Tap Send.
The biggest apps currently supporting this feature are WhatsApp, Google's default SMS app on Nexus phones, and Google Drive. As we see more manufacturers update to Android 6.0, Direct Share is bound to be a feature target for more and more developers.
Question 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?
Start by extending ChooserTargetService
. You'll need to return a List
of ChooserTarget
and how you create those targets is entirely up to you.
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
Now you'll need to declare your ChooserTargetService
in your AndroidManifest
and do two things:
Service
using the android.permission.BIND_CHOOSER_TARGET_SERVICE
permissionIntentFilter
with the android.service.chooser.ChooserTargetService
actionFor example:
<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>
In the Activity
that's going to handle the Intent
, you'll need to add the meta-data
tag android.service.chooser.chooser_target_service
. For example:
<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>
From here, it's mostly a matter of calling Intent.createChooser
and then handling the data if the user chooses your application.
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
Things to note
The ranking score for each ChooserTarget
is used to sort the targets, but is only used if the UI decides to use it. As per ChooserTarget.getScore
The UI displaying the target may take this score into account when sorting and merging targets from multiple sources
Also, as far as I know, this feature isn't actually implemented yet in the Android MNC preview. The ChooserActivity
contains a TODO
for it:
TODO: Maintain sort by ranking scores
When creating a new android.graphics.drawable.Icon
, you'll need to use one of the static
initializers.
Icon.createWithBitmap();
Icon.createWithContentUri()
Icon.createWithData()
Icon.createWithFilePath()
Icon.createWithResource()
Question 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?
The data supplied to ChooserTargetService.onGetChooserTargets
is dynamic. So, there's no direct way to access those items, as far as I know.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With