Android --- send content to another application

Source: Internet
Author: User

When constructing an Intent object, you must specify the operation to be triggered. The Android system defines several operations, including ACTION_SEND. As you guessed, this operation specifies that an Intent object needs an Activity to send data to another Activity, even across process boundaries. To send data to another Activity, you must specify the data and the corresponding data type. Then, the system recognizes the Activity that receives the Intent object, and display them to users (if multiple activities are available), or directly start the corresponding Activity (only one Activity is available ). Similarly, you can specify the data types that your Activity can receive from other applications through your application's inventory file.

Intent objects are used to send and receive data between applications. They are often used for content sharing between social applications. Intent allows users to quickly and easily share information to use their favorite applications.

Note: The best way to add a shared operation is to use the ShareActionProvider class to add the shared operation item to an ActionBar, which is valid in API Level 14.

Send text content

The most direct and commonly used action is the ACTION_SEND operation, which sends text content from an Activity to other activities. For example, the built-in Browser application can share the URL of the currently displayed page as text to any other application. This is good for sharing articles or websites with friends through e-mails or social networks. This type of sharing is implemented in the following example:

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

If the system installs a MIME-type filter with the ACTION_SEND operation and text/plain, the Android system runs the corresponding application. If multiple applications match the request, the system displays a selection dialog box that allows you to select an application that can receive the request. If you call the Intent. createChooser () method, the selection dialog box is always displayed in the Android system. The benefits of doing so are as follows:

1. Even if you have selected the default operation for the Intent, the selected dialog will still be displayed;

2. If no matching application exists, the Android System displays a system message;

3. You can specify a title for the selection dialog box.

The following code is updated:

Intent sendIntent = newIntent ();
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 )));

The displayed result dialog box 1 is displayed.

 

Figure 1. The ACTION_SEND operation dialog box is displayed on the handheld device.

Optional. You can set additional standard information for the Intent: EXTRA_EMAIL, EXTRA_CC, EXTRA_BCC, and EXTRA_SUBJECT. However, if the receiving application is not designed to use this information, nothing will happen. You can also use custom additional information, but it will not be affected unless the application can understand them. Typically, you should use custom additional information defined by the receiving application.

Note: Some email applications, such as Gmail, expect a String [] type of additional information such as EXTRA_EMAIL and EXTRA_CC. You can use putExtra (String, String []) method to add it to your Intent object.

Send binary content

Use the ACTION_SEND operation and set the appropriate MIME type. And place the URL pointing to the data in the additional field named EXTRA_STREAM to share the binary data. This method is often used to share images, but it can be used for any type of binary content:

Intent optional Intent = newIntent ();
Define Intent. setAction (Intent. ACTION_SEND );
Extends Intent. putExtra (Intent. EXTRA_STREAM, uriToImage );
Jsonintent. setType ("image/jpeg ");
StartActivity (Intent. createChooser (Intent, getResources (). getText (R. string. send_to )));

Note the following:

1. You can use the "*/*" MIME type, but this will only match the Activity that can process the normal data stream.

2. The acceptor application must be authorized to access the data pointed to by the Uri. There are many ways to deal with this:

A. Write Data to files in external/shared storage (such as SD card) so that all applications can read the file. Use Uri. fromFile () to create a Uri object that can be passed to the shared Intent. However. Remember that not all applications can process file: // style Uri objects.

B. write data to a file in your own application directory. This file must be created using the openFIleOutput () method with MODE_WORLD_READABLE mode, and then you can use getFileStreamPath () method to return the corresponding File object. Like the method of option A, Uri. fromFile () is used to create A file: // style Uri object for the shared Intent object.

C. images, videos, and audios can be scanned and added to a multimedia file in the system media store. You can use scanFile () to check whether the file exists. onScanCompleted () the callback method returns a Uri object used to share the content: // style contained in the Intent object.

D. the insertImage () method can be used to insert an image to the MediaStore of the system. This method returns a Uri object used to share the content: // style contained in the Intent object.

E. Use your own ContentProvider object to save data and ensure that other applications obtain the correct authorization. Then, you can access your content provider.

Send multiple messages

To share multiple items, use the ACTION_SEND_MULTIPLE operation and the URI list pointing to the content together with the operation. The MIME type varies with the shared content. For example, if you share three JPEG images, the MIME type should be "image/jpeg ". For multiple image types, it should be "image/*", so that the Activity that processes any image type can match it. If you want to share various types of data, use "*/*" as the value. As mentioned above, the receiving application will parse and process your data. For example:

ArrayList <Uri> imageUris = newArrayList <Uri> ();
ImageUris. add (imageUri1); // Add your image URIs here
ImageUris. add (imageUri2 );
 
Intent = new Intent ();
Define Intent. setAction (Intent. ACTION_SEND_MULTIPLE );
Extends Intent. putParcelableArrayListExtra (Intent. EXTRA_STREAM, imageUris );
Specified intent. setType ("image /*");
StartActivity (Intent. createChooser (Intent, "Share images .."));

Make sure that the receiving application can access the data pointed to by the URI.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.