When you construct an intent, you must specify the action you want the intent to "trigger." android defines several actions, including
ACTION_SEND
Which, 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 toActionBar
Is to use
ShareActionProvider
, Which became available in API Level 14.
ShareActionProvider
Is discussed in the lesson about
Adding an Easy Share action.
Send text content
Figure 1.ScreenshotACTION_SEND
Intent chooser on a handset.
The most straightforward and common use ofACTION_SEND
Action 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_SEND
And 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_EMAIL
And
EXTRA_CC
, UseputExtra(String, String[])
To add these to your intent.
Send binary content
Binary data is shared usingACTION_SEND
Action 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 data
Uri
Points 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 createUri
That 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 using
openFileOutput()
With Mode
MODE_WORLD_READABLE
After whichgetFileStreamPath()
Can be used to return
File
. As with the previous option,Uri.fromFile()
Will create
file://
StyleUri
For your share intent.
- Media files like images, videos and audio can be scanned and added to the system
MediaStore
UsingscanFile()
.
onScanCompleted()
Callback returnscontent://
Style
Uri
Suitable for including in your share intent.
- Images can be inserted into the system
MediaStore
Using
insertImage()
Which will returncontent://
Style
Uri
Suitable for including in a share intent.
- Store the data in your own
ContentProvider
, 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_MULTIPLE
Action 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 providedURIs
Point to data that a processing ing application can access.