Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 968
android developer Avatar asked May 28 '15 22:05

android developer


People also ask

What is direct share on my Android phone?

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.

How do I change direct share?

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.

How do I share apps directly?

Tap Manage apps & devices. In the “Overview” tab, next to “Share apps,” tap Send. Select which apps to share. Tap Send.

What apps work with Google direct share?

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.


1 Answers

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.

like image 164
adneal Avatar answered Sep 23 '22 03:09

adneal