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.

Community
  • 1
  • 1
android developer
  • 114,585
  • 152
  • 739
  • 1,270

2 Answers2

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.

adneal
  • 30,484
  • 10
  • 122
  • 151
  • 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? – android developer Jun 09 '15 at 06: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 09 '15 at 14:02
  • What documentation? Currently, I see nothing here about "ChooserTargetService" : http://developer.android.com/preview/api-overview.html#q=ChooserTargetService :( – android developer Jun 09 '15 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 09 '15 at 22:15
  • ok, I see. Since I don't have a real device with Android M, and the emulator lacks in apps that can handle this, I take it that you've tested the code and it works fine, right? Also, about question #2, I've filed a request here: https://code.google.com/p/android-developer-preview/issues/detail?id=2354 – android developer Jun 09 '15 at 22:21
  • @androiddeveloper Yes. That screenshot is from my device. And your request sounds good. – adneal Jun 09 '15 at 22:32
  • 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. – android developer Jun 09 '15 at 22:50
  • btw, you can star the request if you wish . :) – android developer Jun 09 '15 at 22:51
  • Please note, you must have `` in the intent filter of your activity or it will not be able to respond to the implicit intent of the ChooserTargetService. – Reuben Tanner Aug 11 '15 at 18:19
  • Piggybacking on this thread: I'm currently looking into implementing this feature, but I do not understand how it's being linked to the other apps you use and how it's pulling the most frequently used contacts from them. I looked at the Google's code example, but they do not have any of that included there. @adneal any insight into that? Thanks!! – lulu Nov 03 '15 at 03:08
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.

Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216
  • 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. – android developer May 31 '15 at 07:02