郑州网站建设制作价格,企事业网站建设,池州网站制作公,企业自适应网站建设原文地址#xff1a;http://android.xsoftlab.net/training/sharing/receive.html
正如你的程序可以发送数据给其它程序#xff0c;那么你也可以轻松的接收数据。想象一下用户如何与你的程序交互#xff0c;以及你想从其它应用程序接收的数据类型。举个例子#xff0c;一个…原文地址http://android.xsoftlab.net/training/sharing/receive.html
正如你的程序可以发送数据给其它程序那么你也可以轻松的接收数据。想象一下用户如何与你的程序交互以及你想从其它应用程序接收的数据类型。举个例子一个社交网络的程序可能对文本内容更感兴趣比如一个有意思的Web地址Google APP允许接收文本、单张图片或者多张图片。通过这个APP用户可以很轻松的从Android图库APP启动一个Google的发送照片任务。
更新你的清单文件
意图过滤器告知系统可以接收什么样的意图。举个例子如果应用程序可以接收并处理文本内容和任何类型的一张图片或者任何类型的多张图片你的清单文件应该声明成这样
activity android:name.ui.MyActivity intent-filteraction android:nameandroid.intent.action.SEND /category android:nameandroid.intent.category.DEFAULT /data android:mimeTypeimage/* //intent-filterintent-filteraction android:nameandroid.intent.action.SEND /category android:nameandroid.intent.category.DEFAULT /data android:mimeTypetext/plain //intent-filterintent-filteraction android:nameandroid.intent.action.SEND_MULTIPLE /category android:nameandroid.intent.category.DEFAULT /data android:mimeTypeimage/* //intent-filter
/activity Note:有关更多意图过滤器的信息以及意图的分辨请阅读 Intents and Intent Filters 当另一个程序通过构造一个意图并且传递给startActivity()并尝试分享这几种类型的任意一种时你的程序会在意图选择器中列出各种选项。如果用户选择了你的程序那么相应的activity会被启动。然后由你的代码和UI来妥善的处理这些内容。
处理到来的内容
为了处理由Intent传递过来的内容开始调用getIntent()方法获得Intent对象。一旦你获得了这个对象你可以检查其中的内容然后在决定接下来怎么做。记住一点如果这个activity可以由系统的其它部分启动比如系统的桌面然后你需要在执行检查的时候将这种情况考虑在内。
void onCreate (Bundle savedInstanceState) {...// Get intent, action and MIME typeIntent 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) {ArrayListUri imageUris intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);if (imageUris ! null) {// Update UI to reflect multiple images being shared}
} Caution:检查到来的附加数据要格外当心你永远不会知道其它应用程序会发来什么。举个例子比如可能设置了错误的MIME类型或者发送来的图像可能非常的大。还要记得要在单独的线程中处理二进制数据而不是主线程。 更新UI可以像填充一个EditText一样简单否则的话它会像对图片应用有趣图片过滤器一样复杂这需要您的应用程序明确接下来将要做什么。