Develop -- Training(九) -- 分享简单数据
2016-02-23 23:58
351 查看
发送简单的数据给其他应用程序
发送文本内容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);
如果你的设备上安装的应用能够匹配到的行为是 ACTION_SEND 和MIME 类型是 text/plain,Android 系统就会打开这个应用。
如果有多个应用程序匹配它,Android 系统会显示一个对话框,让用户去选择想要打开的应用程序。
注意:如果没有任何应用程序可以匹配的话,也就是没有相应的 Intent,你的应用程序就会报错,所以,我们最好加上系统中有没有这个 Intent 的判断。
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)));
如果你调用 Intent.createChooser() 方法,会出现一个显示选择器。
- 即使用户已经选择了默认操作,仍然会显示选择器。
- 如果没有应用程序匹配,Android 会显示一个系统消息。
- 可以指定选择器对话框的标题。
发送二进制内容
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 为 Intent.ACTION_SEND
携带参数的 URI 数据名称为: Intent.EXTRA_STREAM
如果共享图像数据,MIME 类型为:image/jpeg
如果共享任何类型的数据,MIME 类型为: * / *
发送多件内容
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 为 Intent.ACTION_SEND_MULTIPLE
如果是单个类型的多个数据,如图像类型,MIME 类型为:image/jpeg
如果是图像的类型的混合数据,MIME 类型为:image/*
从其他的应用程序接收简单的数据
更新你的 Manifest为了接收前面所创建的 intent,我们需要在 Manifest 清单文件中做一些配置操作。 需要在 < activity >元素节点 里添加 intent 过滤器,使用< 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>
处理传入的内容
通过 getIntent() 方法得到需要处理的 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 } }
注意:如果是处理二进制数据的话,需要在子线程中处理,而不是在 UI 线程中。
添加一个简单的分享动作
更新菜单声明ShareActionProvider:在API等级14及更高版本。
这个就是 ShareActionProvider 的使用效果。
在 res\menu 资源目录下创建一个 menu 的 xml 文件,在< item >里添加 android:actionProviderClass 属性。
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_item_share" android:showAsAction="ifRoom" android:title="Share" android:actionProviderClass= "android.widget.ShareActionProvider" /> ... </menu>
设置分享意图
private ShareActionProvider mShareActionProvider; ... @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate menu resource file. getMenuInflater().inflate(R.menu.share_menu, menu); // Locate MenuItem with ShareActionProvider MenuItem item = menu.findItem(R.id.menu_item_share); // Fetch and store ShareActionProvider mShareActionProvider = (ShareActionProvider) item.getActionProvider(); // Return true to display menu return true; } // Call to update the share intent private void setShareIntent(Intent shareIntent) { if (mShareActionProvider != null) { mShareActionProvider.setShareIntent(shareIntent); } }
通过 MenuItem.getActionProvider() 得到 ShareActionProvider 实例,然后调用 mShareActionProvider.setShareIntent(shareIntent) 发送意图。
相关文章推荐
- FZU 1978 Repair the brackets
- [leetcode Q9] Container With Most Water
- 17996 Daily Cool Run
- linker command failed with exit code 1 (use -v to see invocation)
- hdu 1021 Fibonacci Again
- 【SPOJ-MAIN75】BST again【计数DP】【记忆化搜索】
- studio调用百度MAP的JNI发生的问题: No implementation found for long com.baidu.platform.comjni.map.commonmemcach
- <init>与<clinit>的区别 http://blog.csdn.net/sunmenggmail/article/details/10211395
- mac mail迁移邮件规则
- error-iis-Service Unavailable
- AIM Tech Round (Div. 1) D. Birthday 数学 暴力
- AIDL
- codeforces 616E. Sum of Remainders 数学
- CritterAI与Recast Navigation寻路
- 217. Contains Duplicate
- POJ 3831 & HDU 3264 Open-air shopping malls(几何)
- POJ 1230 Pass-Muraille#贪心+vector迭代器用法
- pair—模板类型 使用方法
- 谷歌人工智能AlphaGo挑战人类围棋冠军
- 谷歌人工智能AlphaGo挑战人类围棋冠军