Sending content to other apps sends content to other applications

Source: Internet
Author: User

When you construct an intent, you must specify the action you want the intent to "trigger." android defines several actions, including
ACTION_SENDWhich, as you can probably guess, indicates that the intent is sending data from one activity to another, even processing SS process boundaries. to send data to another activity, all you need to do is speicify the data and its
Type, the system will identify compatible indexing ing activities and display them to the user (if there are multiple options) or immediately start the activity (if there is only one option ). similarly, you can advertise the data types that your activities support
Processing ing from other applications by specifying them in your manifest.

Sending and processing ing 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 a share action item toActionBarIs to use
ShareActionProvider, Which became available in API Level 14.
ShareActionProviderIs discussed in the lesson about
Adding an Easy Share action.

Send text content

Figure 1.ScreenshotACTION_SENDIntent chooser on a handset.

The most straightforward and common use ofACTION_SENDAction is sending text content from one activity to another. For example, the built-in browser app can share the URL of the currently-displayed page as text with any application.
This is useful for sharing an article or website with friends via email or social networking. Here is the code to implement 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 there's an installed application with a filter that matchesACTION_SENDAnd MIME type text/plain, the android system will run it; if more than one application matches, the system displays a disambiguation Dialog (a "chooser ")
That allows the user to choose an app. If you callIntent.createChooser()For the intent, Android will
AlwaysDisplay the chooser. This has some advantages:

  • Even if the user has previusly selected a default action for this intent, the chooser will still be displayed.
  • If no applications match, Android displays a system message.
  • You can specify a title for the chooser dialog.

Here's 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));

Http://blog.csdn.net/sergeycao

Optionally, you can set some standard extras for the intent:EXTRA_EMAIL,
EXTRA_CC,EXTRA_BCC,
EXTRA_SUBJECT
. However, if the processing application is not designed to use them, nothing will happen. you can use custom extras as well, but there's no effect unless the processing application understands them. typically, you 'd use M m
Extras defined by the processing application itself.

Note:Some E-Mail applications, such as Gmail, short CT
String[]For extras likeEXTRA_EMAILAnd
EXTRA_CC, UseputExtra(String, String[])To add these to your intent.

Send binary content

Binary data is shared usingACTION_SENDAction combined with setting the appropriate MIME type and placing the URI to the data in an extra named
EXTRA_STREAM. This is commonly used to share an image 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)));

Note the following:

  • You can use a MIME type"*/*", But this will only match activities that are able to handle generic data streams.
  • The processing application needs permission to access the dataUriPoints to. There are a number of ways to handle this:
    • Write the data to a file on external/shared storage (such as the SD card), which all apps can read. Use
      Uri.fromFile()To createUriThat can be passed to the share intent. However, keep in mind that not all applications process
      file://StyleUri.
    • Write the data to a file in your own application directory usingopenFileOutput()With Mode
      MODE_WORLD_READABLEAfter whichgetFileStreamPath()Can be used to return
      File. As with the previous option,Uri.fromFile()Will create
      file://StyleUriFor your share intent.
    • Media files like images, videos and audio can be scanned and added to the system
      MediaStoreUsingscanFile().
      onScanCompleted()Callback returnscontent://Style
      UriSuitable for including in your share intent.
    • Images can be inserted into the systemMediaStoreUsing
      insertImage()Which will returncontent://Style
      UriSuitable for including in a share intent.
    • Store the data in your ownContentProvider, Make sure that other apps have the correct permission to access your provider (or use
      Per-Uri permissions ).
Send multiple pieces of content

To share multiple pieces of content, useACTION_SEND_MULTIPLEAction together with a list of URIs pointing to the content. The MIME type varies according to the mix of content you're sharing. For example, if you share 3 JPEG
Images, the type is still"image/jpeg". For a mixture of image types, it shoshould be
"image/*"To match an activity that handles any type of image. You shoshould only use
"*/*"If you're 'Re sharing out a wide variety of types. As previusly stated, it's up to the processing application to parse and process your data. Here's an example:

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.."));

As before, make sure the providedURIsPoint to data that a processing ing application can access.

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.