Copyright Notice:

This account published articles are from the public account, Chengxiang Moying (cxmyDev), chengxiang Moying all rights reserved.

Every week will be uniformly updated here, if you like, you can follow the public account to get the latest article.

Shall not be reproduced without permission.

One, foreword

For an App, sharing is a common function. The main function of sharing is to enable App to form a self-spreading effect among users, but for developers, sharing is to transfer text, files and other data between different apps.

Using ACTION_SEND or ACTION_SEND_MULTIPLE might be an easy way to simply share content. However, starting with Support V4 22.0.0, a new sharecompatible library, ShareCompat, is introduced to make it easier for developers to construct shared intents.

Second, the ShareCompat

2.1 What is ShareCompat

ShareCompat can help us quickly implement the function of sharing data between applications. Since it is a complete function, it mainly contains two functions: to quickly build an Intent to share, and to quickly read the content shared within other apps.

So, ShareCompat consists of two parts: IntentBuilder and IntentReader. The name suggests their purpose.

/share-class.png

2.2 Sharing Text

To start with, it is very simple to share a plain text using IntentBuilder. IntentBuilder, as its name suggests, is in Builder mode, so it can support multiple method chain calls.

/share-text.png

The most important of these is to set the correct mimeType, which will be an important filter for the intent-filter, indicating that there is knowledge sharing of a plain text type of content.

Calling resolveActivity() to perform a layer of validation before startActivity() is a common safeguard. It avoids the current in the system without any App can match on this implicit Intent, and trigger ActivityNotFoundException exception. Although it is highly unlikely that no text/plain share can be accepted, it is a good coding practice to add it.

2.3 Share HTML text

Some apps, such as email clients, can support HTML-formatted text, which can display richer content than plain text.

/share-html.png

Note that setType() uses text/ HTML and uses the setHtmlText() method to configure shared content.

Since sharing Html content is mostly used to send Email content, the Email address is also configured here.

Generally speaking, the use of Html also requires that the App that receives the shared content supports such content, so it still needs to be used with caution. The App that users share may not be able to display the Html content that we need to share well.

2.4 Send an Email

I also mentioned how to add an Html content to an Email. ShareCompat also provides a good Email sending experience. You can easily construct an Intent to send an Email.

/share-buildclass.png

IntentBuilder provides a very convenient Api to add some Email recipients, cc, etc., all in addEmailXxx() method, very easy to understand, interested in the Api documentation.

2.5 Receive a text content

Although most of the time, we need to develop a feature that supports sharing, it doesn’t affect how we can receive shared content.

The shared content is available in the Intent. IntentReader also provides a very convenient Api to get to its content quickly.

In androidmanifest.xml, you need to configure the incoming Activity and intent-filter.

/share-platxml.png

This is a more general configuration of ShareActivity.

/share-reader.png

IntentReader is also very simple to use. IntentReader is essentially a wrapper around Intent data, providing a convenient Api for extracting data from intents.

2.6 Sharing Files and Pictures

In addition to sharing a piece of text, sometimes you need to share a file (video, image, etc.). Sending these requires additional permissions.

/share-png.png

Constructing an image share is also very easy, just use the setStream() method, pass the Uri of a file, and configure the corresponding mimeType.

However, in this way, we need to consider that Android 6.0 starts to process shared files in strict mode, so we need to use FileProvider to support this.

For FileProvider content, check out the previous article:

Once used on FileProvider only needs to be replaced Uri FileProvider. GetUriForFile () returns the Uri of the can, this is all FileProvider standard content, not detailed here.

The Uri returned by the FileProvider does not explicitly specify the type of mimeType, because it can infer the correct type of mimeType from this Uri.

2.7 Receiving File Contents

Android 6.0 doesn’t change the way files are received, so we just need to use the standard INTentReader.getStream () Api to get shared URIs.

/share-uri.png

What is ShareCompat compatible with

Libraries under the Support V4 package are designed to be compatible with lower Versions of Android, so developers don’t have to pay attention to the differences between them, and we usually see a class with the word Compat in its name, and we can assume that it is a compatible library.

What does ShareCompat do for compatibility between different Versions of Android?

In ShareCompat, there is a static ShareCompatImpl object, which is for compatibility problems. It is an interface, so let’s take a look at its definition.

/share-impl.png

As you can see, it actually does a compatible handling of configureMenuItem() and HTML text escape.

In the static code block of ShareCompat, it is divided into different versions, different versions with different implementation classes.

/share-static.png

In short, it means some processing methods that may not be available on the earlier version, so it will be implemented in a different way. For example, the escapeHtml() method in ShareCompatImplJB calls html.escapehtml (), which is supported only by Api Level 16. So in ShareCompatImplBase, you actually copy its implementation completely, just to do an Html character escape.

/share-copyhtml.png

Four, conclusion

ShareCompat is great for simply sharing content. However, in most cases, we still choose to use the SDK provided by the third-party App for access support, because there will be some customization needs, such as obtaining shared results.

That doesn’t stop us from learning about ShareCompat, though. It’s always helpful to look at code written by Google developers.

Copyright Notice:

This account published articles are from the public account, Chengxiang Moying (cxmyDev), chengxiang Moying all rights reserved.

Every week will be uniformly updated here, if you like, you can follow the public account to get the latest article.

Shall not be reproduced without permission.

Qr code. JPG