Android-send content to another application

Source: Internet
Author: User

This article translated from: http://developer.android.com/training/sharing/send.html

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.
Valid in 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 extends 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 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.