Open In App

Sharing Shortcuts in Android

Improve
Improve
Like Article
Like
Save
Share
Report

With the release of Android Q and its amazing features, it also introduced Sharing Shortcuts. In Q, the Direct Share API has been superseded with the Sharing Shortcut API.

Receiving Data from Simple Ends

An app can not only transmit data to other applications, but it can also receive data from other apps. Consider how users interact with your app and what data types you wish to collect from other apps. For example, a social networking app may be interested in receiving text information from another app, such as an intriguing site URL. Sharing shortcuts, a quicker way to share material from your app, are introduced in Android Q. Think of it as direct share on steroids. This post will explain the distinction between the two and how to use sharing shortcuts.

Making Fabulous Share Targets?

Users will frequently provide data to your app via Android Sharesheet or the intent resolver. The MIME type of all received data is determined by the app that provides it. Your app can accept data supplied by another app in three ways:

  1. An Activity in the manifest with a matched intent-filter tag
  2. One or more ChooserTarget objects are returned by your app’s ChooserTargetService Sharing Shortcuts.
  3. These take precedence over ChooserTarget objects.
  4. Sharing Shortcuts are only accessible if your app is running on Android 10 or higher (API level 29).
<activity android:name=".something.GeeksforGeeks" >
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="image/*" />
    </intent-filter>
</activity>

Aside from that, what exactly is a chooser service?

The share sheet is presented when a user, for example, wishes to share text information from an app. During the sheet preparation process, the system requests specific chooser services to get a list of chooser targets, which can be a regularly emailed contact on your device, a recently active group messaging discussion, or any other relevant item.

Technically, the chooser service is an Android service that is called by the system when a user expressly requests that another app choose a target for an intent, and it provides a list of chooser targets that deep link into the selected app.

<intent-filter>
  <action android:name="android.intent.action.SEND" />
  <category android:name="android.intent.category.DEFAULT" />
  <data android:mimeType="image/*" />
</intent-filter>

When another app tries to share any of these items by creating intent and giving it to startActivity(), your app will be displayed as an option in the Android Sharesheet or intent resolver. If the user chooses your application, the associated activity (.ui.MyActivity in the preceding example) will be launched. It is then up to you to properly manage the material within your code and UI. The App Manifest should include the metadata required to operate with compatible libraries. Do this like:

<activity android:name=".GeeksforGeeks">
    ...
    <meta-data android:name="gfg.sampleApp.gfgShorts"
               android:resource="@xml/shortcuts"/>
</activity>

Take care of the incoming stuff

To deal with the material given by an Intent, use getIntent() to obtain the Intent object. Once you’ve obtained the object, you can study its contents to figure out what to do next. Keep in mind that if this activity may be launched from other areas of the system, such as the launcher, you must account for this when analyzing the purpose.

GeekTip: When a shortcut is set to be long-lived, system services can continue to retrieve it from the cache even after it has been deleted as of a dynamic shortcut.

Now, to test, simply share a text, and then in the sample app, you will see the shortcuts pop-up!

Image 1. The sharing shortcuts


Last Updated : 08 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads