blob: 5bf56268b863f762e229381ae476c1d54827f80b [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?><!--
Copyright 2013 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.
<!-- Include additional dependencies here.-->
This sample demonstrates how to provide the Sharing Shortcuts - Direct Share feature. The app shows
some options directly in the list of share intent candidates and launcher shortcuts.
<template src="base" />
Sample demonstrating how to show some options directly in the list of share intent candidates.
Direct Share is a feature that allows apps to show their internal options directly in the
system Intent chooser dialog. This sample is a dummy messaging app, and just like any other
messaging apps, it receives intents for sharing a plain text. When a user shares some text from some
other app, this sample app will be listed as an option. Using the Direct Share feature, this app
also shows some of contacts directly in the chooser dialog.
Direct Share was [first introduced][1] in Android M where you had to implement a service to provide
direct share targets on demand. The way to do that changed in Android Q, you need to provide your
direct share targets in advance. First, you have to declare share-target elements in the same
application's resource file than [static shortcuts][2]. Then, you need to publish dynamic
shortcuts with the same category you declared in the share-target with the [ShortcutManager API][3].
You will need to manually update the list of shortcuts every time you consider it appropriate.
The API offers methods to update, remove or add shortcuts. You can use the
[ShortcutInfoCompat.Builder][4] to customize your shortcut. If you don't want to block the UI thread
doing these operations, interactions with the ShortcutManager can also happen on a background thread.
There are three ways the app is sending/receiving intents:
- Dynamic Share: The user selected the app in the sharesheet to receive the text. After this,
the user will have to select the contact to share the text with. In our case, the app receives an
intent of type Intent.ACTION_SEND.
- Direct Share: The user selected a person of your app in the sharesheet to share the text with.
The received intent of type Intent.ACTION_SEND will contain a String EXTRA_SHORTCUT_ID that will
have the id of the shortcut that was selected. After this, the app is ready to send the text.
- Launcher shortcut: When the user taps on a launcher shortcut, the intent that was
added to the shortcut will get fired. In our case, it triggers an intent of type Intent.ACTION_DEFAULT.
To make Direct Share backwards compatible with older Android versions, you need to add the AndroidX
sharetarget library and in your AndroidManifest.xml, add a meta-data tag in your Activity that
receives the Intent. Specify android:name as android.service.chooser.chooser_target_service and
android:value as androidx.sharetarget.ChooserTargetServiceCompat.
The way to share text has also changed. Before, you could specify a title in the
[Intent.createChooser()][5] method itself. That is deprecated and unused in Android Q. You can achieve
the same behavior by adding an Intent.EXTRA_TITLE extra to the intent. Similarly, if you want a
preview thumbnail to appear, you can create a content URI and set a ClipData object in the intent.
You can see how to do that in our example, open the file for more details.