关闭

Google Android开发者文档系列-创建有内容分享特性的应用之发送简单数据到其它应用程序

标签: 谷歌Google安卓android文档
272人阅读 评论(0) 收藏 举报
分类:

Sending Simple Data to Other Apps(发送简单数据到其它应用程序)

该系列文章是我在学习Google开发者文档时结合谷歌翻译和自身理解编写的,希望对学习Android开发的朋友带来些便利,由于个人翻译水平有限,所以内容包含原文和译文,希望浏览者结合理解,以免步入我可能错译的误区。在此感谢http://android.xsoftlab.net/提供的镜像,希望转载者注明出处http://blog.csdn.net/u014031072/article/details/51558010方便查看最新博客

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 specify 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.
当你构造了一个intent,你必须定义它想要触发的action。Android定义了一些action,包括ACTION_SEND,正如你可能已经猜到的那样,它表明这个intent要从一个Activity发送数据到另一个Activity,期间甚至跨越进程边界。要将数据发送到另一个Activity,所有你需要做的就是定义数据和它的类型,系统将会识别相应的一些Activity向用户显示出来(如果存在多个选择)或者立即启动这个Activity(如果只有一个选择)。同样,你可以在你的清单文件中通过定义来声明你的Activity支持接收其他应用发过来的什么类型的数据。

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.
通过intent的来实现在应用之间发送和接收数据,对于社会共享来说是最常见的实现方式。intent允许用户使用他们最喜欢的应用程序来简单快速的分享信息。

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.
注:在ActionBar上添加一个分享选项的最好方式是使用ShareActionProvider,ShareActionProvider在API 14及以后可用。ShareActionProvider在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:
ACTION_SEND最直接最常用的方式是从一个Activity发送文本内容到另一个Activity。比如,内置浏览器可以以文本的形式分享当前显示的页面的URL给任何应用程序。这对于通过email或者网络渠道向朋友分享文章和网站来说是有用的。下面是实现这种分享方式的代码:

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.
如果有一个安装的应用程序包含一个filter匹配ACTION_SEND和text / plain的MIME类型,Android系统将会运行它;如果有多个应用程序匹配,系统会显示一个对话框(一个“选择器”)允许用户选择要启动的应用程序。

However, if you call Intent.createChooser(), passing it your Intent object, it returns a version of your intent that will always display the chooser. This has some advantages:
然而,如果你调用Intent.createChooser()方法,传递你的intent对象给他,它总是会显示一个对应你的intent的选择器。这样做有一些好处:

1.Even if the user has previously selected a default action for this intent, the chooser will still be displayed.
1.尽管用户之前为这个intent选择过一个默认的action,选择器仍然会显示。

2.If no applications match, Android displays a system message.
2.如果没有应用程序可以匹配到,Android会显示一个系统信息。

3.You can specify a title for the chooser dialog.
3.你可以给选择器定义一个标题。

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

The resulting dialog is shown in figure 1.
结果对话框如图1所示:

这里写图片描述
图1:关于ACTION_SEND的手机截图

Optionally, you can set some standard extras for the intent: EXTRA_EMAIL, EXTRA_CC, EXTRA_BCC, EXTRA_SUBJECT. If the receiving application is not designed to use them, it simply ignores them.
或者,你可以给intent设置一些额外标准:EXTRA_EMAIL, EXTRA_CC, EXTRA_BCC, EXTRA_SUBJECT. 如果获取intent的应用程序没有打算去使用他们,忽略它们就行。

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.
注:一些email应用程序,比如Gmail,希望得到一个关于类似EXTRA_EMAIL和EXTRA_CC的额外String[]数据。使用putExtra(String, String[]) 方法来添加这些数据到你的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:
二进制数据通过使用ACTION_SEND类型的action结合设置合适的MIME类型并将URI放到名字为EXTRA_STREAM的额外数据中来实现共享。这种方式可以用于分享任何类型的二进制内容,通常用于分享图片:

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:
注意下面的内容:

1.You can use a MIME type of “/“, but this will only match activities that are able to handle generic data streams.
1.你可以使用“* /*”的MIME类型,但这只会匹配能够处理一般数据流的Activity。

2.The receiving application needs permission to access the data the Uri points to. The recommended ways to do this are:
2.接收intent的应用程序需要权限来获取URI所指向的数据。推荐的几种方式如下:

<1>Store the data in your own ContentProvider, making sure that other apps have the correct permission to access your provider. The preferred mechanism for providing access is to use per-URI permissions which are temporary and only grant access to the receiving application. An easy way to create a ContentProvider like this is to use the FileProvider helper class.
<1>在你自己的ContentProvider(内容提供者)中保存这些数据,确保其他应用程序拥有对应的权限来获取你的提供者(的内容)。提供使用权限的较好的机制是使用per-URI权限,他是临时的权限而且只向接收intent的应用提供权限。创建一个这样的ContentProvider(内容提供者)的简易方式是使用FileProvider帮助类。

<2>Use the system MediaStore. The MediaStore is primarily aimed at video, audio and image MIME types, however beginning with Android 3.0 (API level 11) it can also store non-media types (see MediaStore.Files for more info). Files can be inserted into the MediaStore using scanFile() after which a content:// style Uri suitable for sharing is passed to the provided onScanCompleted() callback. Note that once added to the system MediaStore the content is accessible to any app on the device.
<2>使用系统的媒体库。媒体库主要针对视频、音频和图片MIME类型,但是从Android3.0(API level 11)开始也能够存储非媒体类型(查看MediaStore.Files获取更多信息)。在将一个content://类型的合适分享的Uri传给onScanCompleted()回调后,使用sanFile()方法能将文件插入媒体库。注意只要加入了系统媒体库,则加入的内容可以被设备上的任何应用程序获取到。

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:
使用ACTION_SEND_MULTIPLE类型的action结合一个指向具体内容的URI列表来实现分享多个内容的片段。MIME类型根据你要分享的混合内容类型而改变。比如,如果你要分享3张JPEG的图片,MIME类型为”image/jpeg”。对于混合图片类型,MIME类型则应该是”image/“来匹配可以操作任何类型图片的Activity。只有你在分享一个很广泛类型的内容时你才应该使用”/*”。如前面所述,它的MIME类型取决于接收、解析和处理你的数据的应用程序。下面是一个例子:

ArrayList<Uri> imageUris = new ArrayList<Uri>();
// Add your image URIs here在这里添加你的图片的URI
imageUris.add(imageUri1); 
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.
和之前一样,确保接收intent的应用程序能够获取到你提供的URI所指向的数据。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:24357次
    • 积分:497
    • 等级:
    • 排名:千里之外
    • 原创:16篇
    • 转载:61篇
    • 译文:1篇
    • 评论:2条