共享数据 之 从其他APP接收数据
2013-03-27 14:12
561 查看
你的APP可以发送数据给其他的APP,那么也可以很容易的从其他的APP接收数据。要考虑用户如何和你的APP交互,你的APP需要接收哪类数据。比方说一个社交网络APP,可能会对text类型的数据感兴趣,比方说一个网站的URL地址等。例如Google+ Android application就接收各种txt文本和单张或者多张图片。利用这个APP,用户可以很方便的将图库里面的图片传到google+.
修改你的Manifest文件
intent filter告诉系统,这个APP可以接收处理的intent类型。比方说创建一个
注:想要了解更多,阅读:Intents and Intent Filters
当其他的程序通过一个intent来传递要分享的数据,把这个intent作为
要处理intent传递进来的内容,首先要调用
警告:当检查用extra获取的数据的时候,你永远不知道别的APP会发送什么数据给你。比方说,一个错误的MIME可能被设置,或者是发送的图片非常大。要记得处理二进制数据的时候,要记得和主UI线程分开,不要在主UI线程里面进行。
更新UI可以简单的更新
修改你的Manifest文件
intent filter告诉系统,这个APP可以接收处理的intent类型。比方说创建一个
ACTION_SEND动作的intent,那么如果你希望接收这个intent,那么就需要在想要接收的APP里面的对应的activity里面创建一个intent filter,来声明接收这个动作。你需要在你的manifest文件里面定义这个intent filter,,用<intent-filter>标签来定义。如果你的APP希望可以处理text文本信息,可以接收任何类型格式的一张或者多张图片,那么你的manifest里面的intent filter应该这么写:
[code]<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>
注:想要了解更多,阅读:Intents and Intent Filters
当其他的程序通过一个intent来传递要分享的数据,把这个intent作为
startActivity()参数,启动的时候,如果你的程序可以接收这个intent的动作和要处理的数据,你的程序就会显示在一个可供用户选择的一个对话框里面,因为可能有多个应用都可以处理这个intent.如果选择了你的APP,那么对应的你的APP里面的activity就会启动(如上面.ui.MyActivity会被启动),然后就可以在你的这个activity里面来合适的处理这个数据了。
处理传进来的内容
要处理intent传递进来的内容,首先要调用
getIntent()来获取之前传递内容的那个intent.当获取到这个对象的时候,下来就可以考虑根据里面的内容做对应的处理。要记住,如果你的activity可以被系统的其他部分启动,比方说launcher,那么你就需要在检查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 } }
警告:当检查用extra获取的数据的时候,你永远不知道别的APP会发送什么数据给你。比方说,一个错误的MIME可能被设置,或者是发送的图片非常大。要记得处理二进制数据的时候,要记得和主UI线程分开,不要在主UI线程里面进行。
更新UI可以简单的更新
EditText内容,也可以很复杂,如处理一张图。这个动作直接明确了你的APP将会产生的变化。
相关文章推荐
- android接收从其他App传送来的数据
- android 应用间共享数据,调用其他app数据资源
- 从其他App接收简单的数据
- android 接收从其他App传送来的数据
- 接收从其他app返回的数据
- 背水一战 Windows 10 (101) - 应用间通信: 通过协议打开指定的 app 并传递数据以及获取返回数据, 将本 app 沙盒内的文件共享给其他 app 使用
- 和其他App分享简单的数据(2)从其他App中接收简单的数据
- Android Receiving Content from Other Apps [从其他APP接收分享数据]
- 与应用程序或其他 DLL 共享自己 DLL 中的数据
- 关于使用TCP或者其他链接方式时数据接收问题
- APP数据共享
- Android通过sharedUserId机制实现APK间共享数据,App的换肤可以通过此机制实现
- 数据共享之其他可供选择的手段
- 在 App 扩展和主 App 间共享数据
- android学习(十) 从其他应用接收简单数据
- iOS app之间共享数据的几种方法
- Struts2学习总结(2)--数据接收,数据共享,类型转换,上传下载
- 接收get或post数据使用fwrite写入文件中,方便追踪错误;或其他几种缓存方式
- 宽带能连接上,接收不到数据或局域网内某台电脑的共享资源无法访问解决办法
- 关于ContentProvider数据共享与其他共享方式区别的理解