博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Receiving Content from Other Apps 接收来自其他应用程序的内容
阅读量:4046 次
发布时间:2019-05-24

本文共 3941 字,大约阅读时间需要 13 分钟。

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 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.  http://blog.csdn.net/sergeycao

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 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 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:

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

When another application tries to share any of these things by constructing an intent and passing it to , 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.

Handle the Incoming Content

To handle the content delivered by an , start by calling to get 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.

void onCreate (Bundle savedInstanceState) {    ...    // Get intent, action and MIME type    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    }    ...}void handleSendText(Intent intent) {    String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);    if (sharedText != null) {        // Update UI to reflect text being shared    }}void handleSendImage(Intent intent) {    Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);    if (imageUri != null) {        // Update UI to reflect image being shared    }}void handleSendMultipleImages(Intent intent) {    ArrayList
imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); if (imageUris != null) { // Update UI to reflect multiple images being shared }}

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.

Updating the UI can be as simple as populating an , 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.

你可能感兴趣的文章
数据库索引介绍及使用
查看>>
MongoDB数据库插入、更新和删除操作详解
查看>>
MongoDB文档(Document)全局唯一ID的设计思路
查看>>
mongoDB简介
查看>>
nodejs 浏览器弹窗下载图片 data:image/jpeg;base64示例
查看>>
JAVA实现AES加密
查看>>
JAVA实现DES加密
查看>>
关于AES256算法java端加密,ios端解密出现无法解密问题的解决方案
查看>>
node.js AES/ECB/PKCS5Padding 与其他语言的加密解密通用
查看>>
Java and Nodejs on AES
查看>>
AES加密CBC模式兼容互通四种编程语言平台【PHP、Javascript、Java、C#】
查看>>
js老生常谈之this,constructor ,prototype
查看>>
nodejs-post文件上传原理详解
查看>>
node-formidable详解
查看>>
CenOS Linux安装nginx
查看>>
Nginx 关于 Rewrite 执行顺序详解
查看>>
Linux网络编程socket错误分析
查看>>
CAS实现SSO单点登录原理
查看>>
MongoDB中的_id和ObjectId
查看>>
美团酒店Node全栈开发实践
查看>>