This is especially important for text data. Tapping on any Direct Share target should take the user to an interface where an action can be directly performed on the target's subject. Avoid showing users a disambiguation or placing them in an interface that is not related to the tapped target. Specifically, do not take the user to a contact disambugation interface where they must confirm or reselect the contact to share with, since they've already done that by tapping the target in the Android Sharesheet. For example, in a messaging app, tapping a Direct Share target should take the user to a conversation view with the person they selected.
The keyboard should be visible and the message should be prefilled with the shared data. Intent filters inform the system what intents an application component is willing to accept. For example, if your application handles receiving text content, a single image of any type, or multiple images of any type, your manifest would look like:. When another application tries to share any of these things by constructing an intent and passing it to startActivity , your application will be listed as an option in the Android Sharesheet or intent resolver.
If the user selects your application, the corresponding activity. MyActivity in the example above will be started.
It is then up to you to handle the content appropriately within your code and UI. Note: For more information on intent filters and intent resolution please read Intents and Intent Filters. To handle the content delivered by an Intent , call getIntent to get the Intent object. Once you have the object, you can examine its contents to determine what to do next.
Keep in mind that if this activity can be started from other parts of the system, such as the launcher, then you will need to take this into consideration when examining the intent. Take extra care to check the incoming data, you never know what some other application may send you. For example, the wrong MIME type might be set, or the image being sent might be extremely large.
Also, remember to process binary data in a separate thread rather than the main "UI" thread.
Updating the UI after receiving the data can be as simple as populating an EditText , or it can be more complicated like applying an interesting photo filter to an image. It's up to your app what happens next. Your app is represented by its icon and label in the Android Sharesheet and intent resolver.
These are both defined in the manifest. You can set activity or intent filter labels to provide more context. Icons set on intent-filter and activity tags will be ignored. Note: The best share targets do not need a label and icon in the associated activity or intent filter. The receiving app's name and icon alone should be enough for users to understand what will happen when sharing. Direct Share was introducted in Android 6. Results were retrieved reactively on demand, leading to a slow loading time for targets. Instead of retrieving results reactively on demand, the Sharing Shortcuts API lets apps publish direct share targets in advance.
This is the preferred way to publish both Sharing Shortcuts and ChooserTargets. See the instructions below. You can only use dynamic shortcuts to publish Direct Share targets. Follow these steps to publish direct share targets using the new API:. Builder includes new and enhanced methods that provide additional info about the share target:. Specifies whether or not a shortcut is valid when it has been unpublished or made invisible by the app as a dynamic or pinned shortcut. If a shortcut is long lived, it can be cached by various system services even after if has been unpublished as a dynamic shortcut.
Making a shortcut long lived can improve its ranking. See Get the best ranking for details. Associates one or more Person objects with the shortcut.
Use Content Resolver ("content://sms/inbox") to read SMS which are in inbox. .. This post is a little bit old, but here is another easy solution for getting data related to SMS content provider in Android: Use this lib: . sconthyomonsi.ga How can I read SMS messages from the device programmatically in Android?.
This can be used to better understand user behavior across different apps, and to help potential prediction services in the framework provide better suggestions in a ShareSheet. Adding Person info to a shortcut is optional, but strongly recommended if the share target can be associated with a person.
Note that some share targets, such as cloud, cannot be associated with a person. Including a specific Person object with a unique key in a sharing target and related notifications can improve its ranking. For a typical messaging app, a separate shortcut should be published for each contact, and the Person field should contain the contact's info. If the target can be associated with multiple people like a group chat , add multiple Person objects to a single share target. When publishing a shortcut to an individual person please include their full name in setLongLabel and any short name, such as a nickname or a first name, in setShortLabel.
For an example of publishing Sharing Shortcuts see the Sharing Shortcuts sample code. The Android Sharesheet shows a fixed number of Direct Share targets. These suggestions are sorted by rank. You can potentially improve the ranking of your shortcuts by doing the following:.
If we can't access the APN, we have to fall back and use. MmsMessage processReceivedPdu final Context context ,. This method shows you how to read from the Android SharedPreferences. You may unsubscribe from these newsletters at any time. If these 2 lines appeared in the log, it means that your application works well, that is your software is able to send SMS messages.
To make a Sharing Shortcut, you'll need to add an image via setIcon. Sharing Shortcuts can appear across system surfaces and may be reshaped. To ensure your shortcut looks as intended, provide an adaptive bitmap through IconCompat. Adaptive bitmaps should follow the same guidelines and dimensions set for adaptive icons. The most common way to accomplish this is to scale the intended square bitmap to 72x72dp and center that within a xdp transparent canvas.
Do not provide imagery masked to a specific shape. Share targets must be declared in the app's resource file, similar to static shortcuts definitions. The XML code looks something like this:. The data element in a share target is similar to the data specification in an intent filter. Each share target can have multiple categories, which are only used to match an app's published shortcuts with its share target definitions. Categories can have any arbitrary app-defined values.
In case the user selects the Sharing Shortcut in the Android Sharesheet that matches with the example target-share above, the app will get the following share intent:. If the user opens the share target from the launcher shortcuts, the app will get the intent that was created when adding the sharing shortcut to the ShortcutManagerCompat. Since it's a different intent, Intent. However, the link from the share activity to the service must be taken into account as a chooser target provider.
In the following example, the implementation of ChooserTargetService is androidx. This makes the process of retrieving direct share targets much faster when preparing the ShareSheet.
From the app developer's point of view, when using the new API, the app needs to provide the list of direct share targets ahead of time, and potentially update the list of shortcuts every time the internal state of the app changes for example, if a new contact is added in a messaging app. So your published share targets may get buried under other apps' share targets and potentially never appear when sharing. Don't do it! Curious how you might test the same flow with a real device?
So, with this new command at our disposal, we can now construct a simple test to verify the flow we discussed:. It's pretty fun to watch a fake text message show up on the emulator screen! Of course, if we wanted, we could also switch apps to the Messages app itself, and verify other properties of the message that came through. Anyway, a full code sample involving the flow above is below or you can check it out on GitHub :. Edition MobileBy; import io. AndroidDriver; import java. IOException; import java. URL; import org. Test; import org.
RunWith; import org. JUnit4; import org. By; import org. The final piece of the puzzle is a simple API that can wrap the whole thing up and also hide the fact that this is Android specific. Using a class like this is generally good practice as it allows us flexibility with the actual underlying native interface.
Next time I will wrap this all up with the user experience and package everything into an easy to use cn1lib. Opinions expressed by Java Code Geeks contributors are their own. Start the discussion Views Tweet it! Do you want to know how to develop your skillset to become a Java Rockstar?