21

Background

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:

enter image description here

Or, at least that's what I've understood from it.

The question

I have 2 questions regarding this new feature:

  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?

  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?

    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.

4
  • For that matter, where's the implementation guide for anything aside from Permissions, Auto Backup and App Links?! Hopefully on their way... Jun 4, 2015 at 23:22
  • 1
    android-developers.blogspot.ru/2015/09/… Here is a sample app by Google
    – Denis Nek
    Sep 24, 2015 at 20:16
  • @DenisNek Sadly it has issues of importing. Sep 24, 2015 at 21:28
  • @android developer it's easy to fix. Just create simple FrameLayout with ListView inside.
    – Denis Nek
    Sep 26, 2015 at 6:35

2 Answers 2

17

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:

  1. Bind the Service using the android.permission.BIND_CHOOSER_TARGET_SERVICE permission
  2. Include an IntentFilter with the android.service.chooser.ChooserTargetService action

For 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

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.

10
  • Where did you get all of this information? Is there a tutorial? A sample? Maybe something on Github ? Have you checked it out? Also, about question #2, why does it matter that it's dynamic? I do the query code anyway, so I do want to get the newest data, and I do assume that it can change... Checking which activities of which apps can handle an intent is already possible, so why shouldn't it be possible to get this list too? Jun 9, 2015 at 6:41
  • 1
    @androiddeveloper I extended ChooserTargetService and read the Javadoc. You can also download the source and documentation for the Android-MNC preview from the SDK manager. As far as question 2 goes, the framework provides a way to query the activities that handle a particular Intent. But it doesn't provide a way to query the data returned in ChooserTargetService.onGetChooserTargets. That data is loaded on demand and is controlled by each individual app that takes advantage of the new API.
    – adneal
    Jun 9, 2015 at 14:02
  • What documentation? Currently, I see nothing here about "ChooserTargetService" : developer.android.com/preview/… :( Jun 9, 2015 at 21:46
  • @androiddeveloper Google hasn't updated the docs on developer.android.com yet. Download the source and or docs through the SDK Manager.
    – adneal
    Jun 9, 2015 at 22:15
  • 1
    ok I will now tick your answer. I hope to remember to get back to it when I have an M version of Android. Thank you for everything. Jun 9, 2015 at 22:50
0

I have different understanding of this future.

Until now when user wanted to share something they been asked to choose the application they want to share with, and then this application handled the share.

Now instead of user choosing the application they will choose the content from the application that will handle the share. Each such option is encapsulated in android.service.chooser.ChooserTargetService.

So as you see on the image, it shows some products of ChooserTargetService, the user see some contacts ui without lunching or sharing just yet.

I believe your dialog could be triggered at the same way.

1
  • What you wrote is a part of the question itself.... The question is how to use it ? How to provide the images and texts ? I see contacts images and names... Also the opposite. May 31, 2015 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.