Android sends data to other apps

When constructing an intent, you must specify the operation you want the intent to "trigger". Android defines several operations, including ACTION_SEND, as you might guess, indicating that the intent is to send data from one Activity to another, even across process boundaries. To send data to another Activity, all you need to do is specify the data and its type, the system will recognize the compatible receiving Activity and display it to the user (if there are multiple options) or start the Activity immediately (if there is only one option ). Similarly, you can advertise the types of data your Activity supports from other applications by specifying other applications in the manifest.

Sending and receiving data between applications with intents is most commonly used for social sharing of content. Intents allow users to share information quickly and easily using their favorite applications.

Note: The best way to add shared action items to the ActionBar is to use ShareActionProvider, which is available in API level 14.

Send text data

The most direct and common usage of the ACTION_SEND operation is to send text content from one Activity to another Activity. For example, the built-in browser application can share the URL of the currently displayed page as text with any application. This is useful for sharing articles or websites with friends via email or social networks. Here is the code that implements this type of sharing:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);

If the installed application has a filter that matches ACTION_SEND and the MIME type text / plain, the Android system will run it; if more than one application matches, the system displays a disambiguation dialog that allows the user to select the application ("selector") .

However, if you call Intent.createChooser (), passing its Intent object, it returns a version of the intent that will always display the selector. This has some advantages:

  • Even if the user previously selected a default action for this intent, the selector will still be displayed.
  • If no application matches, Android displays a system message.
  • You can specify a title for the selector dialog.

Here is the updated code:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));

1
The generated dialog box.

Alternatively, you can set some standard additional features for intent: EXTRA_EMAIL, EXTRA_CC, EXTRA_BCC, EXTRA_SUBJECT. If the receiving application is not designed to use them, it just ignores them.

Note: Some email applications (such as Gmail) require a String [] as additional functions such as EXTRA_EMAIL and EXTRA_CC, please use putExtra (String, String []) to add them to your intent.

Send binary data

Use the ACTION_SEND operation and set the corresponding MIME type and place the data URI in the extra named EXTRA_STREAM to share the binary data. This is usually used to share images, but can be used to share any type of binary content:

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));

Please note the following:

  • You can use the MIME type "* / *", but this will only match Activities that can handle generic data streams.

The receiving application needs permission to access the data pointed to by Uri. The recommended method is:

  • Store the data in your own ContentProvider and ensure that other applications have the correct permissions to access your provider. The preferred mechanism for providing access is to use temporary per-URI permissions and only grant access to the receiving application. An easy way to create such a ContentProvider is to use the FileProvider auxiliary class.
  • Use the system MediaStore. MediaStore mainly targets video, audio, and image MIME types, but starting with Android 3.0 (API level 11), it can also store non-media types (for more information, see MediaStore.Files). You can use scanFile () to insert the file into the MediaStore, and then pass the content: // style Uri suitable for sharing to the provided onScanCompleted () callback. Please note that once added to the system MediaStore, any application on the device can access the content.

Send multiple content

To share multiple content, use the ACTION_SEND_MULTIPLE operation and a list of URIs pointing to the content. The MIME type varies depending on the combination of content you share. For example, if you share 3 JPEG images, the type is still "image / jpeg". For a mixture of image types, it should be "image / " to match the activity of processing any type of image. You should only use " / *" if you have shared various types. As mentioned earlier, it is up to the receiving application to parse and process your data. Here is an example:

ArrayList<Uri> imageUris = new ArrayList<Uri>();
imageUris.add(imageUri1); // Add your image URIs here
imageUris.add(imageUri2);

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share images to.."));

As before, make sure the provided URI points to data that the receiving application can access.

Published 80 original articles · Like 319 · Visits 340,000+

Guess you like

Origin blog.csdn.net/jimo_lonely/article/details/53447998