您的位置:首页 > 移动开发 > Android开发

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

2016-06-06 11:36 701 查看

Receiving Simple Data from Other Apps

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

Just as your application can send data to other applications, so too can it easily receive data from applications. Think about how users interact with your application, and what data types you want to receive from other applications. For example, a social networking application would likely be interested in receiving text content, like an interesting web URL, from another app. The Google+ Android application accepts both text and single or multiple images. With this app, a user can easily start a new Google+ post with photos from the Android Gallery app.

跟你的应用能够发送数据到其它应用一样,它也能轻松的接收来自其它应用发送的数据。想想用户如何与你的应用交互,你的应用想从其他应用接收什么类型的数据。比如,一个网络社交应用可能对从其他应用接收发送过来的文字内容,比如一个有趣的网页URL,感兴趣。Google+应用接收文字和一张或多张图片。通过这款app,用户可以通过发送Android图库应用中的照片来轻松的开启一个新的Google+。

Update Your Manifest(更新你的项目清单)

Intent filters inform the system what intents an application component is willing to accept. Similar to how you constructed an intent with action in the Sending Simple Data to Other Apps lesson, you create intent filters in order to be able to receive intents with this action. You define an intent filter in your manifest, using the intent-filter element. For example, if your application handles receiving text content, a single image of any type, or multiple images of any type, your manifest would look like:

意图过滤器告知系统应用程序愿意接受什么类型的意图。与发送简单的数据到其他应用程序课程中说到的如何构建一个带有action的intent相似,你创建intent-filter是为了去接收具有该action的intent。在你的清单文件中通过<?(去掉问号)intent-filter>要素来定义一项intent filter。例如,如果你的应用程序接收处理文本内容、一个任何格式的图片,或多个任何意型的图片,你的清单文件应该像这个样子:

<activity android:name=".ui.MyActivity" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>


Note: For more information on intent filters and intent resolution please read Intents and Intent Filters

注:了解更多关于intent 和intent filter 的内容,请阅读Intents and Intent Filters

When another application tries to share any of these things by constructing an intent and passing it to startActivity(), your application will be listed as an option in the intent chooser. If the user selects your application, the corresponding activity (.ui.MyActivity in the example above) will be started. It is then up to you to handle the content appropriately within your code and UI.

当另外的应用试图去通过构建一个intent并将其传递给startActivity()方法的方式来分享任意上面例子中类型的内容,你的应用会被列在intent选择器列表中。如果用户选择你的应用,则相应的activity(上面例子中为.ui.MyActivity)将会启动。然后由你通过代码和UI界面来处理相应的内容。

Handle the Incoming Content(处理发送过来的数据)

To handle the content delivered by an Intent, start by calling getIntent() to get Intent object. Once you have the object, you can examine its contents to determine what to do next. Keep in mind that if this activity can be started from other parts of the system, such as the launcher, then you will need to take this into consideration when examining the intent.

为了处理intent传递过来的内容,你应该从调用getIntent()来获得传递过来的Intent对象开始,一旦你获取到这个对象,你可以查看它的内容来决定下一步怎么做。请记住,如果这个activity可以从系统的其他部分启动,如启动器,那么你在检查这个意图时,将需要考虑到这一点(判断intent来自哪里,来进行对应操作)。

void onCreate (Bundle savedInstanceState) {
...
// Get intent, action and MIME type
//获取intent、action和MIME类型
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent); // Handle text being sent 处理文字内容
} else if (type.startsWith("image/")) {
handleSendImage(intent); // Handle single image being sent 处理图片内容
}
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
if (type.startsWith("image/")) {
handleSendMultipleImages(intent); // Handle multiple images being sent 处理多张图片内容
}
} else {
// Handle other intents, such as being started from the home screen 处理其他intent,比如从home界面开启的intent
}
...
}
void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
// Update UI to reflect text being shared
//更新UI来显示分享的文本
}
}
void handleSendImage(Intent intent) {
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imageUri != null) {
// Update UI to reflect image being shared
//更新UI来显示分享的图片
}
}
void handleSendMultipleImages(Intent intent) {
ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (imageUris != null) {
// Update UI to reflect multiple images being shared
//更新UI来显示分享的多张图片
}
}


Caution: Take extra care to check the incoming data, you never know what some other application may send you. For example, the wrong MIME type might be set, or the image being sent might be extremely large. Also, remember to process binary data in a separate thread rather than the main (“UI”) thread.

注意:要特别小心检查输入的数据,你永远不知道其他一些应用程序可能会发送给你什么数据。例如,可能设置错误的MIME类型,或被发送的图像可能会非常大。此外,请记住在单独的线程而不是主(“UI”)线程来处理二进制数据。

Updating the UI can be as simple as populating an EditText, or it can be more complicated like applying an interesting photo filter to an image. It’s really specific to your application what happens next.

更新UI可以是填充一个EditText一样的简单操作,或者可以是复杂的像给图片施加一个有趣的滤镜的操作。这取决于你的应用程序接下来会发生什么。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: