Use Sharesheet to share Android app link

Use Sharesheet to share Android app link

Rich link sharing with Sharesheet

Android App Links allows your application to open web links instead of using a web browser. Handling these deep links was covered in our Introductory Deep Linking course. In addition to being able to open deep links, your application should also be able to create and share deep links to specific content.

In Android, sharing linked content within an app is an important operation. Link sharing enables your application and its users to collaborate, communicate, social network, and more. All of these activities increase user engagement with your app and take full advantage of your app's deep linking support. A great tool for sharing linked content is Android Sharesheet.

https://developer.android.com/training/sharing/send

This article shows why you should Sharesheet, and demonstrates how to use it in your linked content. let's start!
First of all, what is it Sharesheet? Android provides an in-app interface for selecting users or applications to send content directly. This interface is called Sharesheetand is displayed when an intent with ACTION_SENDan action Intent.createChooser. SharesheetProvides an efficient way to share data with other applications without using explicit intents.

The example below will call Sharesheet, to present the user with related goals for sharing. Each user will have forms appropriate to their installed applications and contacts.

val sendIntent: Intent = Intent().apply {
    
    
    action = Intent.ACTION_SEND
    putExtra(Intent.EXTRA_TEXT, "This is my text to send.")
    type = "text/plain"
}

val shareIntent = Intent.createChooser(sendIntent, null)
startActivity(shareIntent)


This is a basic example, we are more likely to share more interesting content than plain text. If your app supports Android App Links, it can share links directly to content inside the app. Let's look at an example link: "https://example.com/specials/spaghetti". The link is clear to the app's users that it's a pasta special, and we probably don't need to modify the share form.

However, sometimes the link's target may not be clear enough. For example, a link to a Google Doc usually looks like this: https://docs.google.com/document/d/1TejHHDrz…NotARealLink. With links alone, users may not know what information they are actually sharing. In this case, we should consider adding a richer preview.

Coincidentally, this is also a guideline from the Android docs https://developer.android.com/training/sharing/send.

Consider sharing a complicated URL like: 
https://www.google.com/search?ei=2rRVXcLkJajM0PEPoLy7oA4. 
A richer preview can reassure your users what is being shared.

For our use, richer previews include additional title text and image thumbnails. We can set the title text Intent.EXTRA_TITLEby . To Sharesheetembed an image in , we clipDataset the parameter of the Intent to the content provided through the file provider URI. The rest of this article guides you through providing Sharesheetricher previews of shared content in .

AndroidManifest.xmlFirst things first: let's create a file provider in our file.

<provider
   android:name=".AndroidFoodFileProvider"
   android:authorities="com.example.fileprovider"
   android:exported="false"
   android:grantUriPermissions="true">
   <meta-data
       android:name="android.support.FILE_PROVIDER_PATHS"
       android:resource="@xml/filepaths" />
</provider>

Now, we need to filepaths.xmladd files to our XML resource directory. For our example, we will serve images from the root directory or the cache directory. This filepaths.xmlfile will link our cache root directory to the content URI /images.

<paths xmlns:android="http://schemas.android.com/apk/res/android">
   <cache-path name="images" path="/" />
</paths>

Although not strictly necessary, as required by the FileProvider API documentation, we should create a stub implementation for our application. Quote: "It is possible to use FileProvider directly instead of extending it. However, on some devices this is not reliable and can cause crashes."

import androidx.core.content.FileProvider

class AndroidFoodFileProvider : FileProvider() {
    
    
}

So what does this actually do? We declare a file provider that creates content URIs for images in our cache directory. We can call FileProvider.getUriForFileto create a URI that the shared form will use to display the image.

import androidx.core.content.FileProvider

class AndroidFoodFileProvider : FileProvider() {
    
    
}

With the image's link, title and content URI in hand, we are now ready to create Intent.

val sendIntent: Intent = Intent().apply {
    
    
       val destination = "https://example.com/specials/spaghetti"
       putExtra(Intent.EXTRA_TITLE, "Link Description")
       val imageUri = FileProvider.getUriForFile(context, "com.example.fileprovider", File(context.cacheDir, "spaghetti.jpg"))
       clipData = ClipData.newUri(context.contentResolver, "", imageUri)
       flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
       putExtra(Intent.EXTRA_TEXT, destination)
   }

val shareIntent = Intent.createChooser(sendIntent, null)
context.startActivity(shareIntent)

The following is a great sharing link effect
shareSheet with title&image

reference link

https://medium.com/androiddevelopers/the-deep-links-crash-course-part-1-introduction-to-deep-links-2189e509e269
https://developer.android.com/reference/androidx/core/content/FileProvider

Guess you like

Origin blog.csdn.net/u011897062/article/details/131221938