Direct Share.

Tram Ho

Direct Share is a feature that allows an application to share content to other applications. Users can then access your app directly when sharing content to another app.

Direct Share works with the concept of Sharing Shortcuts. An app proposes to pre-publish shareable targets, published Sharing Shortcuts will be kept by the system until the app updates them, or the app is uninstalled. When displayed to the user, the system ranks any applicable shortcuts using a prediction service, showing the shortcuts more likely to be used.

Setup and requirements:

In order to use this feature, the minimum complieSDK is 29. Direct Share uses Sharing Shortcuts so you have to create a file shortcuts.xml

The shortcuts.xml file needs to be declared in an activity whose intent filter is set to action android.intent.action.MAIN and category android.intent.category.LAUNCHER in the AndroidManifest.xml file.

Use the ShortcutManager API

Once the shared target elements are defined, we need to publish the dynamic shortcuts matching those definitions to the ShortcutManager API. Sharing shortcuts remain in the system until they are updated by the same or uninstalled app.

build.gradle

SharingShortcutsManager is the class in charge of interacting with ShortcutManager. Every time the user opens the app we will push the saved sharing shortcuts.

MainActivity.kt

We can configure the same applications with Direct Share

Here I use share text / plain style.

SharingShortcutsManager.kt

Create shareable lists.

The app only publishes four shortcuts due to a limited shortcut. Never add more shortcuts than what is defined in ShortcutManagerCompat.getMaxShortcutCountPerActivity .

To create the name and icon:

SharingShortcutsManager.kt

If a shortcut persists, it can be cached by various system services and may appear as a share target even if it has not been unpublished or deleted by the application. .

Publish sharing

SharingShortcutsManager.kt

AndroidManifest.xml

SendMessageActivity.kt

And the end result.

Thank you for watching

Code lab: https://developer.android.com/codelabs/android-direct-share#0

Share the news now

Source : Viblo