应用间的数据接收和发送
来源:互联网 发布:用ps淘宝详情页怎么做 编辑:程序博客网 时间:2024/04/23 16:39
一 简单数据的发送
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 hereimageUris.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拿到数据处理就好了
阅读全文
0 0
- 应用间的数据接收和发送
- 数据发送和接收
- TCP的数据发送和接收
- TCP的数据发送和接收
- socket数据的接收和发送
- kafka的数据发送和接收java_API
- Windows下数据的接收和发送
- 8、socket数据的接收和发送
- kafka的数据发送和接收java_API
- mailbox数据发送和接收的过程
- BlackBerry通过全局变量的发送和接收来更新应用界面数据
- WSAAsyncSelect发送和接收数据
- WSAAsyncSelect发送和接收数据
- L2CAP数据发送和接收
- LWIP 数据接收和发送
- QTcpSocket的连续发送数据和连续接收数据
- QTcpSocket的连续发送数据和连续接收数据
- UDP传输协议的基本应用-发送和接收
- spark2.1 和 python3.6的问题
- 欢迎使用CSDN-markdown编辑器
- 异常Error:Connection timed out: connect. If you are behind an HTTP proxy, please configure the proxy
- AutoCompeleteTestView--动态自动匹配输入的内容
- 由中间向两边渐变的分割线
- 应用间的数据接收和发送
- Andriod通过经纬度解析实际地址
- MySQL监控利器-Innotop
- Git中三种文件状态及其转换
- 上传工具类
- Scala学习笔记系列<1>
- Instant Run使用要注意的坑
- 39. Combination Sum
- 使用IntelliJ IDEA开发SpringMVC网站(四)用户管理