Receiving Content from Other Apps 接收来自其他应用程序的内容
来源:互联网 发布:linux搭建测试环境 编辑:程序博客网 时间:2024/05/16 12:39
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. TheGoogle+ 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. 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 actionACTION_SEND
in the Send Content to Other Apps Using Intents 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:
<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 readIntents and Intent Filters
When another application tries to share any of these things by constructing an intent and passing it tostartActivity()
, 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 Intent
, start by callinggetIntent()
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.
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<Uri> 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 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.
- Receiving Content from Other Apps 接收来自其他应用程序的内容
- Android Receiving Content from Other Apps [从其他APP接收分享数据]
- Receiving Content from Other Apps
- Android---接收来自其他应用程序的内容
- Sending Content to Other Apps 将内容发送到其他应用程序
- Receiving Simple Data from Other Apps
- 《Training:Receiving Simple Data from Other Apps》
- Android基础---接收来自其他应用程序的内容
- android Sending Content to Other Apps[ 发送分享的数据到其他App]
- 收到来自其他应用程序的简单的数据
- PhoneFactory.getDefaultPhone from apps other than PhoneApp - msg#01738
- Blind Receiving盲接收
- Android Wear Preview- 从通知上接收语音输入(Receiving Voice Input from a Notification)
- SQL Update内容 来自其他表
- Interacting with Other Apps
- 接收来自MQ的消息
- 跟Google学写代码:Interacting with Other Apps【Capture Photo from phone】
- android-Interacting with Other Apps
- viterbi译码-一图胜千言
- 解决ntp的错误 no server suitable for synchronization found
- 我想这个很牛逼
- 两种跳转方式分别是什么?有什么区别?
- myeclipse安装svn插件
- Receiving Content from Other Apps 接收来自其他应用程序的内容
- 基于LwIP socket的UDP服务器
- EL表达式详解
- 左旋字符串源码
- java操作文件工具类
- 如果更改SimpleButton 按钮中的文本
- wget命令详解
- 小创新,不同业务场景使用不同的map
- 学科前沿技术(人工智能与智能系统中的先驱人物)