和其他App分享简单的数据(2)从其他App中接收简单的数据
2015-01-05 16:19
375 查看
更新我们的Manifest
Intent Filter 通知系统什么样的Intent是一个app组件愿意接收的。我们创建Intent Filter为了能够接收特定的action的Intent。比如这个例子哦,我们的Activity可以接收三种类型的action
Intent Filter 通知系统什么样的Intent是一个app组件愿意接收的。我们创建Intent Filter为了能够接收特定的action的Intent。比如这个例子哦,我们的Activity可以接收三种类型的action
<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>处理传进来的内容
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 } }注意!!!我们要小心检查传递过来的数据,我们永远不知道其他app将会传递什么数据进来。例如:可能一个错误的MIME类型的数据被传递过来了,或者是特别大的图片将会被传递过来。当然啦,记得开启其他子线程处理我们二进制文件哦,千万不要在主(UI)线程中处理二进制文件。
相关文章推荐
- 和其他App分享简单的数据(1)给其他App发送简单的数据
- Android Receiving Content from Other Apps [从其他APP接收分享数据]
- 和其他App分享简单的数据(3)添加一个简单的分享Action
- Android官方开发文档Training系列课程中文版:分享简单数据之从其它APP接收简单数据
- 从其他App接收简单的数据
- 接收从其他app返回的数据
- android 接收从其他App传送来的数据
- 共享数据 之 从其他APP接收数据
- Android - 分享内容 - 接收其他APP的内容
- 【Android Training - 06】分享数据内容 [Lesson 1 - 发送分享的数据到其他App]
- android Sending Content to Other Apps[ 发送分享的数据到其他App]
- android 从其他app接收分享的内容
- android给其他App发送简单的数据
- 【Android Training - 06】分享数据内容 [Lesson 1 - 发送分享的数据到其他App]
- ContentProvider(4):怎样获取其他APP分享的数据
- android接收从其他App传送来的数据
- Google Training 建立分享内容的APP ------ 分享简单的数据
- android学习(十) 从其他应用接收简单数据
- Google Android开发者文档系列-创建有内容分享特性的应用之接收其它应用程序发送的简单数据
- Android官方开发文档Training系列课程中文版:分享简单数据之发送简单数据给其它APP