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 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 ActionBar
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
The most straightforward and common use of the ACTION_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 matches ACTION_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 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_EMAIL
and
EXTRA_CC
, use putExtra(String, String[])
to add these to your intent.
Send Binary Content
Binary data is shared using the ACTION_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 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
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 create the Uri
that 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_READABLE
after which getFileStreamPath()
can be used to return a
File
. As with the previous option, Uri.fromFile()
will create a
file://
style Uri
for your share intent.
- Media files like images, videos and audio can be scanned and added to the system
MediaStore
using scanFile()
. The
onScanCompleted()
callback returns a content://
style
Uri
suitable for including in your share intent.
- Images can be inserted into the system
MediaStore
using
insertImage()
which will return a content://
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, use the ACTION_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 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 URIs
point to data that a receiving application can access.
分享到:
相关推荐
Android最重要的特性之一就是:一个应用可以基于不同的 action,把用户推送到其他应用中去。比如说,如果你想在你的应用中显示一张带有商店地址的地图,你并不需要在自己的应用中去实现一个显示地图的Activity,而是...
能够为需要进行内容备份的程序场景提供一体化支持:集合了基于任务调度的内容备份,内容压缩,远程发送,命令执行等等功能。而这一切,仅需进行简单的配置管理即可完成。 请根据用途选择: EasyBackup Application: ...
Google Apps Script is a cloud-based scripting language based on JavaScript to customize and automate Google applications. Apps Script makes it easy to create and publish add-ons in an online store ...
You will learn the concepts of creating a menu, sending mails, building interactive web pages, and implementing all these techniques to develop an interactive Web page as a form to submit sheets ...
You will learn the concepts of creating a menu, sending mails, building interactive web pages, and implementing all these techniques to develop an interactive Web page as a form to submit sheets ...
说到发送邮件发送,先提一下SMTP(呵呵,高手就跳过这一段吧!)。 SMTP的全称是“Simple Mail Transfer Protocol”,即简单邮件传输协议。它是一组用于从源地址到目的地址传输邮件的规范,通过它来控制邮件的中转方式...
基于C#+ASP.NET实现的Email Sending Web Service邮件服务程序源码。
This is a complete email sending client in Visual Basic
This example is a very simple solution to sending and receiving data to and from a Parallax Basic Stamp. This example requires a Basic Stamp and the MSComm control. Also included is an example Basic ...
操作系统课程作业(本人亲自...b) 子进程调用函数write()向父进程发送自己的进程ID和字符串” s sending a message to parent.\n”。 c) 父进程调用函数read()通过管道读出子进程发来的消息,将消息输出屏幕,然后终止
NULL 博文链接:https://sap.iteye.com/blog/832453
兔子工人 为支持我关于“兔子工人”的讨论而创建的存储库。 如果您说葡萄牙语,则可以单击查看有关如何使用RabbitMQ中的worker应用程序概念的视频...发布者应用程序发送一条消息 Worker应用程序启动并接收发布的消息
编制一段程序,实现进程的管道通信,使用系统调用pipe()建立一个管道文件;两个子进程P1和P2 分别向管道各写一句话: Child1 is sending a message! Child2 is sending a message! 而父进程则从管道中读出来自于...
The JMS API stands for Java Message Service Application Programming Interface, and it is used by applications to send asynchronous business-quality messages to other applications. In the messaging ...
相信不少筒子在刷机解决wife和蓝牙错误时都是这样一键root或者是adb,但是有时却会出现sending boot FAILED,窗口秒退的情况,本人浏览各大论坛提问的不少,回答的却都没有能解决问题,自己一番摸索后得到找到一个很...
gem install resque-sentry用法将以下内容添加到初始化程序: require 'resque/failure/multiple'require 'resque/failure/redis'require 'resque-sentry'# [optional] custom logger value to use when sending to ...
安装Visual Studio 2012 Express之后,双击打开web.config文件时经常出现“There was a problem sending the command to the program”的错误,然后VS2012 Express打开了,但web.config文件没打开,需要再次双击web....
I remember, once I was to develop an application, which sends emails at the specific time to specific people with the customized messages. For that I developed a COM service that used MAPI. MAPI, i.e...
Step by Step for configuration of sending customize IDOC SAP自定义IDOC类型发送接收处理详细步骤
Because printing a source code file that's 50 percent meat and 50 percent fat adds bulk to a book without adding content. Some of the code produced by the MFC AppWizard in Visual C++ 6.0 won't even ...