Share apps with other android

Sharing Content between Android apps

Sharing is caring, as they say, but sharing on Android means something perhaps slightly different. ‘Sharing’ is really shorthand for sending content such as text, formatted text, files, or images between apps.

So if ‘sharing’ == sending content, it makes slightly more sense that it is implemented using ACTION_SEND (or ACTION_SEND_MULTIPLE) Intents and its dozen extras.

While that approach is perfectly valid, I prefer to use ShareCompat, a set of classes in the v4 Support Library designed to make it easy to build intents for sharing content.

Sharing text

Sharing plain text is, as you might imagine, a good place to start. In fact, there’s not a whole lot to it:

ShareCompat.IntentBuilder uses a fluent API where you can chain together multiple method calls, using only the ones you need. For sharing, one of the most important parts is picking the right mime type — this is how apps filter what type of content they can receive. By using text/plain, we signify that our Intent will only contain plain text. Then, of course, setText() is how we actually add the CharSequence to the Intent to send. And while you can certainly send styled text using setText(), there’s no guarantee that the receiving app will honor that styling, so you should ensure that the text is legible with or without styling.

You’ll note we then use resolveActivity() before calling startActivity(). As mentioned in Protecting Implicit Intents with Runtime Checks, this is critical to prevent an ActivityNotFoundException when there is no Activity available to handle the mime type you have selected. While probably not as much of a concern with text/plain, it may be much more common with other types.

Note: when you use startActivity(shareIntent), that respects any default apps the user has set (i.e., if they’ve previously selected sharing all “text/plain” items to a certain app). If you’d like to instead always show a disambiguation chooser, use the intent generated from IntentBuilder.createChooserIntent() as explained in the ACTION_CHOOSER documentation.

Sharing HTML text

Some apps, most notably email clients, also support formatting with HTML. The changes, compared to plain text, are fairly minor:

The differences here are that we use of setHtmlText() in place of setText() and a mime type of text/html replacing text/plain. Here ShareCompat actually does a little bit extra: setHtmlText() also uses Html.fromHtml() to create a fallback formatted text to pass along to the receiving app if you haven’t previously called setText() yourself.

Given that many of the apps that can receive HTML text are email clients, there’s a number of helper methods to set the subject, to:, cc:, and bcc: email addresses as well — consider adding at least a subject to any share intent for best compatibility with email apps.

Читайте также:  Как настроить рингтон андроид

Of course, you’ll still want to call resolveActivity() just as before — nothing changes there.

Receiving text

While the focus so far has been on the sending side, it is helpful to know exactly what is happening on the other side (if not just to build a simple receiving app to install on your emulator for testing purposes). Receiving Activities add an intent filter to the Activity:

The action is obviously the more critical part — without that there’s nothing that would denote this as an ACTION_SEND (the action behind sharing). The mime type, same as with our sending code, is also present here. What isn’t as obvious are the two categories. From the element documentation:

Note: In order to receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter. The methods startActivity() and startActivityForResult() treat all intents as if they declared the CATEGORY_DEFAULT category. If you do not declare it in your intent filter, no implicit intents will resolve to your activity.

So CATEGORY_DEFAULT is required for our use case. Then, CATEGORY_BROWSABLE allows web pages to natively share into apps without any extra effort required on the receiving side.

And to actually extract the information from the Intent, the useful ShareCompat.IntentReader can be used:

Similar to IntentBuilder, IntentReader is just a simple wrapper that make it easy to extract information.

Sharing files and images

While sending and receiving text is straightforward enough (create text, include it in Intent), sending files (and particularly images — the most common type by far) has an additional wrinkle: file permissions.

The simplest code you might try might look like

And that almost works — the tricky part is in getting a Uri to the File that other apps can actually read, particularly when it comes to Android 6.0 Marshmallow devices and runtime permissions (which include the now dangerous READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions).

My plea: don’t use Uri.fromFile(). It forces receiving apps to have the READ_EXTERNAL_STORAGE permission, won’t work at all if you are trying to share across users, and prior to KitKat, would require your app to have WRITE_EXTERNAL_STORAGE. And really important share targets, like Gmail, won’t have the READ_EXTERNAL_STORAGE permission — so it’ll just fail.

Instead, you can use URI permissions to grant other apps access to specific Uris. While URI permissions don’t work on file:// URIs as is generated by Uri.fromFile(), they do work on Uris associated with Content Providers. Rather than implement your own just for this, you can and should use FileProvider as explained in the File Sharing Training.

Once you have it set up, our code becomes:

Using FileProvider.getUriForFile(), you’ll get a Uri actually suitable for sending to another app — they’ll be able to read it without any storage permissions — instead, you are specifically granting them read permission with FLAG_GRANT_READ_URI_PERMISSION.

Note: we don’t call setType() anywhere when building our ShareCompat (even though in the video I did set it). As explained in the setDataAndType() Javadoc, the type is automatically inferred from the data URI using getContentResolver().getType(uriToImage). Since FileProvider returns the correct mime type automatically, we don’t need to manually specify a mime type at all.

If you’re interested in learning more about avoiding the storage permission, consider watching my Forget the Storage Permission talk or at least go through the slides, which covers this topic in depth at 14:55 (slide 11).

Читайте также:  Как быстро запустить андроид

Receiving files

Receiving files isn’t too different from text because you’re still going to use ShareCompat.IntentReader. For example, to make a Bitmap out of an incoming file, it would look like:

Of course, you’re free to do whatever you want with the InputStream — watch out for images that are so large you hit an OutOfMemoryException. All of the things you know about loading Bitmaps still apply.

The Support Library is your friend

With both ShareCompat (and its IntentBuilder and IntentReader) and FileProvider in the v4 Support Library, you’ll be able to include sharing text, HTML text, and files in your app with the best practices by default.

Источник

How to use Nearby Share on your Android phone

Source: Joe Maring / Android Central

In June 2020, Google launched a feature that Android users have been anxiously awaiting for years — its answer to Apple’s AirDrop. It came in the form of something called «Nearby Share,» and it allows you to easily share links to other Android users through a streamlined and simple process. Nearby Share is now widely rolling out to all Android phones, and if you aren’t quite sure how to use it, we’ll be walking you through all of its ins and outs. Let’s get started!

Products used in this guide

  • Android goodness: Google Pixel 4a ($350 at Amazon)

How to use Nearby Share on your Android phone

To get the ball rolling, we’ll take a look at how you initiate Nearby Share on your phone to share something with a contact. It’s just as easy as sharing something through any other app on your phone, with the whole process going as follows:

    Tap the share icon on something you want to share (it looks like three circles with lines connecting them).

Swipe up on the Android share menu.

Source: Android Central

Nearby Share will look for a contact to share your link with.

Source: Android Central

Tap the phone you want to share with.

Source: Android Central

You can use Nearby Share to share links, files, and other items on your phone, so feel free to play around with it and use it to share all kinds of things. You can even use Nearby Share to share items from the best Android phones to the best Chromebooks.

How to accept a Nearby Share on your Android phone

That’s how you share something with someone else, but what does the whole process look like from the other angle? If you’re receiving a file via Nearby Share, this is what you’ll see.

    Tap the Nearby Share notification pop-up.

Tap Turn on.

Source: Android Central

Tap Accept to accept the share.

Source: Android Central

Once again, we’re treated to a very clean and simple user interface, making Nearby Share wonderfully easy to use.

How to send apps via Nearby Share on your Android phone

A recently-added feature to Nearby Share is the ability to send apps from Google Play to other (nearby) Android users.

  1. Open the Google Play app from the phone you wish to share from.
  2. Tap on the menu icon in the top left corner (3 lines).

Tap on My apps & games.

Source: Android Central

From the receiver’s phone, follow the same steps as above, but tap Receive.

Source: Android Central

From the sender’s phone, select the receiver you wish to send apps to.

Source: Android Central

The Receiver must then confirm the pairing code.

Source: Android Central

The Receiver will select which app(s) to install.

Source: Android Central

Now the apps will quickly be delivered from the sender to the receiver. And best of all, you don’t need to be on the same Wi-Fi or cellular network to share apps — it all works over Bluetooth!

How to customize Nearby Share on your Android phone

Now that you know how to use Nearby Share, it’s time to start customizing the feature so that it works exactly the way you want it to. There are a few different things you can tweak, and they’re all worth messing around with.

  1. Open the Settings app on your phone.
  2. Scroll down and tap Google.
  3. Tap Device connections.

Tap Nearby Share.

Source: Android Central

Tap Device name to change the name of your device when sharing something.

Source: Android Central

Tap Data usage to choose if you want to use data, Wi-Fi, or share offline.

Source: Android Central

Those last two settings are perhaps the most important, as they greatly impact how you can use Nearby Share. If you’re sharing a sensitive file that you don’t want to get in the hands of the wrong person accidentally, you can limit Nearby Share to only work with select contacts that you choose. Furthermore, if you don’t want other Android users to be able to send you things, you can make your phone hidden from the service entirely.

The data usage page is also quite interesting, especially the option that allows you to exclusively share files without an internet connection. This will slow down the process (especially for larger files), but it’s a nice option to have if you’re trying to watch your data usage.

Our top equipment picks

Android goodness

Google Pixel 4a

Get new Android features before everyone else

If you want a phone that’ll get the latest Android updates as soon as they’re released, the Pixel 4a is for you. Along with being backed by that excellent software support, the Pixel 4a also delivers a gorgeous AMOLED display, a top-notch camera, and buttery smooth performance.

We may earn a commission for purchases using our links. Learn more.

These are the best rugged Android phones

Living the rough and tumble life? Get yourself a smartphone that can handle everything you throw at it — or throw your phone at.

These are the best cases for the Samsung Galaxy Z Flip 3 smartphone

The Galaxy Z Flip 3 exists in more colors than you could imagine. You should show off and protect those colors with a quality case. These are the best cases from Samsung, Spigen, Incipio, and beyond.

These are the best Google Pixel 6 Pro screen protectors for your phone

Your Google Pixel 6 Pro deserves the best, so check out our roundup of some of the best screen protectors for your phone.

Источник

Читайте также:  Социал клаб для андроид
Оцените статью