Android开发者文档笔记(二)
来源:互联网 发布:开淘宝企业店铺多少钱 编辑:程序博客网 时间:2024/05/21 11:21
***分享简单的数据
**两个Application数据
*发送文本内容
ntent 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,VIEW..然而,这种动作的申明是一对多的,也就是上面代码中的ACTION_SEND动作执行时,会有多个Activity对应,系统就会提供一个列表,供使用者选择。或者自己实例化一个选择框:
Intent sendIntent = new Intent();sendIntent.setAction(Intent.ACTION_SEND);//EXTRA_EMAIL, EXTRA_CC, EXTRA_BCC, EXTRA_SUBJECTsendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");sendIntent.setType("text/plain");startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
*发送二进制文件
</pre></p><p><pre name="code" class="java">//发送一张图片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)));//发送图片数组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.."));
*为一个Activity添加多个意图:
<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>
*接收文件:
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 }}
对于二进制文件来说,有可能会比较大,android官方建议使用独立线程进行接收,如有必要,再显示到UI中
*对于ActionBar上的菜单编写:
//实现分享功能
<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;...@Overridepublic 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 intentprivate void setShareIntent(Intent shareIntent) { if (mShareActionProvider != null) { mShareActionProvider.setShareIntent(shareIntent); }}
0 0
- Android开发者文档笔记(二)
- Android开发者文档笔记(一)
- Android开发者文档笔记(三)
- Android文档笔记:通知(二)
- Android文档笔记:通知(二)
- FloodLight开发者文档二(译文)
- 读安卓开发者文档笔记
- Android的开发者文档
- Android文档笔记:服务(二)-创建…
- Google protobuf开发者文档笔记
- Android开发者上手宝典(二)
- 离线使用Android开发者文档
- Android 开发者笔记(OP)
- ProGaurd官方文档学习笔记(二)
- 微信公共开发者文档 阅读笔记
- 有米Android SDK开发者文档
- android培训文档提纲(二)
- Android开发者必知的java知识(二)Annotation
- POJ 2195:Going Home
- 【codevs1851】越狱,快速幂数学
- 《乔丹传奇》 留下深刻印象的文字
- 黑马程序员之JAVA反射
- 北大OJ2084
- Android开发者文档笔记(二)
- 轻松学习JavaScript十二:JavaScript基于面向对象之创建对象(二)
- 算法导论序言
- 棋盘
- iOS通信加密
- (Win8、Win7)MAK激活密钥分享【资源有限】
- SQLite初级操作
- PHP常见出错及解决方案(5)
- linux字符截取