应用间的数据接收和发送
2017-05-19 16:14
253 查看
一 简单数据的发送
1.1 发送文本内容
Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send."); sendIntent.setType("text/plain"); startActivity(sendIntent);
也就是通过Intent.ACTION_SEND发送出去,同时设置好数据和类型,key是EXTRA_TEXT。
还可以通过调用Intent.createChooser()方法,它可以设置弹出选择应用框是的标题:
Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send."); sendIntent.setType("text/plain"); startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
标题就是R.string.send_to的内容了。
1.2发送二进制内容
Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage); shareIntent.setType("image/jpeg"); startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
同样的是通过ACTION_SEND发送出去,数据key 是 EXTRA_STREAM ,类型是"image/jpeg"
。当然还是使用ContentProvider好。
1.3发送多个内容块
ArrayList<Uri> imageUris = new ArrayList<Uri>(); imageUris.add(imageUri1); // Add your image URIs here imageUris.add(imageUri2); Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE); shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris); shareIntent.setType("image/*"); startActivity(Intent.createChooser(shareIntent, "Share images to.."));
这个时候ACTION换成了:ACTION_SEND_MULTIPLE,类型"image/*"。
二 应用数据的接收
2.1.更新你的manifest
<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>
也就是设置接收数据的action:"android.intent.action.SEND"和<data/>。
2.2.处理发送来的内容
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 } }
从getIntent拿到数据处理就好了
相关文章推荐
- BlackBerry通过全局变量的发送和接收来更新应用界面数据
- Google Android开发者文档系列-创建有内容分享特性的应用之接收其它应用程序发送的简单数据
- 利用ASP发送和接收XML数据的处理方法
- 利用ASP发送和接收XML数据的处理方法
- c#中UDP数据发送和接收
- 异步SOCKET编程-发送和接收数据[转]
- 异步SOCKET编程-发送和接收数据[转]
- 异步 SOCKET 编程 - 发送和接收数据
- 使用UDP协议发送和接收数据
- 使用UDPClient发送和接收数据
- 利用ASP发送和接收XML数据的处理方法与代码
- Asp.net Socket 客户端(发送和接收数据
- 使用UDP非连线式发送接收数据(聊天室模式)
- Socket网络编程学习笔记(5):发送和接收实体类数据
- asp.net中应用Socket传递接收数据
- 闪烁led来标志串口的接收与发送数据
- [C#] 接收和发送UDP数据
- [C#] 接收和发送UDP数据
- 利用ASP发送和接收XML数据的处理方法
- 地磅称量系统之(60) 接收地磅发送过来的数据显示在LCD上