从其他App接收简单的数据
2016-11-23 23:04
295 查看
1、配置Intent-Filter
Intent-Filters可以告知系统可以接收什么样的Intent,我们需要在AndroidManifest.xml中为我们的Activity配置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>
如上配置,我们的Activity可以处理单个的文本、单个的图片、多个图片。
2、处理接收到的数据
为了处理Intent传递过来的数据,我们可以通过getIntent方法获取Intent实例,一旦拿到这个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
}
}注意:一定要好好的检查接收的数据,因为你永远都不会知道其他应用会给你发什么数据,比如被错误的设置了MIME Type,或者图片非常大,因此谨记要在子线程中处理二进制数据,而不是UI线程。
Intent-Filters可以告知系统可以接收什么样的Intent,我们需要在AndroidManifest.xml中为我们的Activity配置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>
如上配置,我们的Activity可以处理单个的文本、单个的图片、多个图片。
2、处理接收到的数据
为了处理Intent传递过来的数据,我们可以通过getIntent方法获取Intent实例,一旦拿到这个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
}
}注意:一定要好好的检查接收的数据,因为你永远都不会知道其他应用会给你发什么数据,比如被错误的设置了MIME Type,或者图片非常大,因此谨记要在子线程中处理二进制数据,而不是UI线程。
相关文章推荐
- 和其他App分享简单的数据(2)从其他App中接收简单的数据
- android给其他App发送简单的数据
- android 接收从其他App传送来的数据
- 和其他App分享简单的数据(3)添加一个简单的分享Action
- android接收从其他App传送来的数据
- 先其他app发送简单数据
- Android Receiving Content from Other Apps [从其他APP接收分享数据]
- 共享数据 之 从其他APP接收数据
- Android官方开发文档Training系列课程中文版:分享简单数据之从其它APP接收简单数据
- 和其他App分享简单的数据(1)给其他App发送简单的数据
- 接收从其他app返回的数据
- android学习(十) 从其他应用接收简单数据
- php学习笔记(三十)ajax请求和接收参数的实现方式(包括json数据格式的简单处理)
- WIN32API串口接收数据简单测试
- @RequestBody和@ResponseBody的简单使用接收JSON(接收json数据)
- 【Android Developers Training】 33. 接收来自其它应用的简单数据
- 基于Zetta(Node.js)的数据接收端server中,可接收数量不定传感数据的IoT APP实现
- C# Post数据和接收简单示例【摘】
- C# Post数据和接收简单示例【转】
- C#工业物联网和集成系统解决方案的技术路线(数据源、数据采集、数据上传与接收、ActiveMQ、Mongodb、WebApi、手机App)