Android Share Action (1)------Share data

Source: Internet
Author: User
Tags gettext home screen

I. Sharing of data-sharing text data:

The most direct use of action_send is to send text content from one activity to another activity. For example, the Android built-in browser can share the URL of the currently displayed page as text content to other programs. This feature is useful for sharing articles or URLs to friends via email or social networks.

Intent sendintent = new Intent (); sendintent.setaction (intent.action_send); Sendintent.putextra (Intent.extra_text, " This was my text to send. "); Sendintent.settype ("Text/plain"); StartActivity (sendintent);

If Intent.createchooser () is called for intent, then Android will always show up for selection. This has some benefits:

    • Even if the user has previously set the default action for this intent, the selection screen will be displayed.
    • If there are no matching programs, Android displays system information.
    • We can specify the title of the selection interface.
Intent sendintent = new Intent (); sendintent.setaction (intent.action_send); Sendintent.putextra (Intent.extra_text, " This was my text to send. "); Sendintent.settype ("Text/plain"); StartActivity (Intent.createchooser (Sendintent, Getresources (). GetText ( r.string.send_to));

As follows:
  

Share a binary file

Sharing binaries (slices) is similar: Sharing binary data requires a combination of setting a specific MIME type ,需要在 Extra_stream ' inside the URI to place the data, there is an example of sharing the image below, which can also be modified to share any type of binary data:

Intent sendintent = new Intent (); sendintent.setaction (intent.action_send); Sendintent.putextra (Intent.extra_text, " This was my text to send. "); Sendintent.settype ("Text/plain"); StartActivity (Intent.createchooser (Sendintent, Getresources (). GetText ( r.string.send_to));
Send multiple contents (send multiple Pieces of content)

In order to share many different types of content at the same time, you need to use ACTION_SEND_MULTIPLE a URIs list that is assigned to those data. MIME types vary depending on the blended content you share. For example, if you share a picture of 3 jpeg, the MIME type is still image/jpeg . If it is a different image format, it should be used image/* to match activity that can receive any type of image. If you need to share many different types of data, you can use it */* to represent mime. As described earlier, this depends on the programs that are received to parse and process our data. You need to ensure that you have access to the URL.

arraylist<uri> Imageuris = new arraylist<uri> (); Imageuris.add (IMAGEURI1); Add your image URIs Hereimageuris.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.));
Two. Receive data 1. Update our manifest file

The main is to set intent fliters to tell the system is willing to receive those data.

2. Processing the received data

The data is transmitted through intent, so the Getintent () method is called to get to the intent object. When we get to this object, we can judge the data on the surface, and decide the next behavior.

void OnCreate (Bundle savedinstancestate) {    ...    Get intent, action and MIME type    Intent intent = getintent ();    String action = Intent.getaction ();    String type = Intent.gettype ();    if (Intent.ACTION_SEND.equals (ACTION) && type! = null) {        if ("Text/plain". Equals (Type)) {            //Handle text Being sent        } else if (Type.startswith ("image/")) {            //Handle single image being sent        }    } else if (Intent . Action_send_multiple.equals (Action) && type! = null) {        if (Type.startswith ("image/")) {            //Handle Multiple images being sent        }}    else {        //Handle other intents, such as being started from the home screen
   }    ...}

 

 

Android Share Action (1)------Share data

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.