Search code examples
androidandroid-intentandroid-manifestandroid-6.0-marshmallow

On Android M, how to configure the "direct-share" capabilities (image, text), and how to query the items?


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.


Solution

  • 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.