Sending Content to Other Apps 将内容发送到其他应用程序
2013-04-27 09:43
281 查看
When you construct an intent, you must specify the action you want the intent to "trigger." Android defines several actions, including
type, the system will identify compatible receiving 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
receiving from other applications by specifying them in your manifest.
Sending and receiving 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 to an
Adding an Easy Share Action.
The most straightforward and common use of the
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:
If there's an installed application with a filter that matches
that allows the user to choose an app. If you call
always display the chooser. This has some advantages:
Even if the user has previously 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:
Optionally, you can set some standard extras for the intent:
extras defined by the receiving application itself.
Note: Some e-mail applications, such as Gmail, expect a
Note the following:
You can use a MIME type of
The receiving application needs permission to access the data the
Write the data to a file on external/shared storage (such as the SD card), which all apps can read. Use
Write the data to a file in your own application directory using
Media files like images, videos and audio can be scanned and added to the system
Images can be inserted into the system
Store the data in your own
per-URI permissions).
images, the type is still
As before, make sure the provided
ACTION_SENDwhich, as you can probably guess, indicates that the intent is sending data from one activity to another, even across 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 receiving 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
receiving from other applications by specifying them in your manifest.
Sending and receiving 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 to an
ActionBaris 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. Screenshot ofACTION_SENDintent chooser on a handset.
The most straightforward and common use of the
ACTION_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 matches
ACTION_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 call
Intent.createChooser()for the intent, Android will
always display the chooser. This has some advantages:
Even if the user has previously 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 receiving application is not designed to use them, nothing will happen. You can use custom extras as well, but there's no effect unless the receiving application understands them. Typically, you'd use custom
extras defined by the receiving application itself.
Note: Some e-mail applications, such as Gmail, expect a
String[]for extras like
EXTRA_EMAILand
EXTRA_CC, use
putExtra(String, String[])to add these to your intent.
Send Binary Content
Binary data is shared using theACTION_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 of
"*/*", but this will only match activities that are able to handle generic data streams.
The receiving application needs permission to access the data the
Uripoints 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 create the
Urithat can be passed to the share intent. However, keep in mind that not all applications process a
file://style
Uri.
Write the data to a file in your own application directory using
openFileOutput()with mode
MODE_WORLD_READABLEafter which
getFileStreamPath()can be used to return a
File. As with the previous option,
Uri.fromFile()will create a
file://style
Urifor your share intent.
Media files like images, videos and audio can be scanned and added to the system
MediaStoreusing
scanFile(). The
onScanCompleted()callback returns a
content://style
Urisuitable for including in your share intent.
Images can be inserted into the system
MediaStoreusing
insertImage()which will return a
content://style
Urisuitable 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, use theACTION_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 should be
"image/*"to match an activity that handles any type of image. You should only use
"*/*"if you're sharing out a wide variety of types. As previously stated, it's up to the receiving application to parse and process your data. Here's an example:
ArrayList<Uri> imageUris = new ArrayList<Uri>(); imageUris.add(imageUri1); // Add your image URIs here imageUris.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 provided
URIspoint to data that a receiving application can access.
相关文章推荐
- android Sending Content to Other Apps[ 发送分享的数据到其他App]
- Receiving Content from Other Apps 接收来自其他应用程序的内容
- Sending Content to Other Apps
- 让其他的App唤起自己的App——Allowing OtherApps to Start Your Activity
- Sending Simple Data to Other Apps
- 《Training:Sending Simple Data to Other Apps》
- Android Receiving Content from Other Apps [从其他APP接收分享数据]
- 内容共享 之 发送内容到其他的APP
- Android---把内容发送给另外一个应用程序
- 163邮箱本地测试发送邮件成功服务器报Sending the email to the following server failed : smtp.163.com:25
- java发送邮件附件.网页.文本类型的实现(Content的内容设计)
- 重新想象 Windows 8 Store Apps (3) - 控件之内容控件: ToolTip, Frame, AppBar, ContentControl, ContentPresenter; 容器控件: Border, Viewbox, Popup
- Allowing Other Apps to Start Your Activity
- android学习(九) 发送简单数据到其他应用程序
- APPS:How to view the concurrent request output of other users
- 发送邮件时候遇到了这样的一个错误!Sending the email to the following server failed : smtp.163.com:25
- (六)Interacting with Other Apps与其他应用进行交互
- [Android教程]通过Intent分享数据内容给其他应用程序
- #256 – 使用FixedDocument 在指定的地方显示内容(Use a FixedDocument to Display Content at Fixed Locations)
- 【Android Training - 06】分享数据内容 [Lesson 1 - 发送分享的数据到其他App]