Android官方培训课程学习(六): Android分享简单数据
2016-10-08 14:42
477 查看
给其他App发送简单的数据
分享文本内容(Send Text Content)
ACTION_SEND最直接常用的地方是从一个Activity发送文本内容到另外一个Activity。intent调用了Intent.createChooser(),那么Android总是会显示可供选择。下面是一段Sample Code:
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); startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to));
另外,我们可以为intent设置一些标准的附加值,例如:EXTRA_EMAIL, EXTRA_CC, EXTRA_BCC, EXTRA_SUBJECT等。然而,如果接收程序没有针对那些做特殊的处理,则不会有对应的反应
分享二进制内容(Send Binary Content)
分享二进制的数据需要结合设置特定的MIME类型,需要在EXTRA_STREAM里面放置数据的URI,下面有个分享图片的例子,该例子也可以修改用于分享任何类型的二进制数据: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)));
发送多块内容(Send Multiple Pieces of Content)
为了同时分享多种不同类型的内容,需要使用ACTION_SEND_MULTIPLE与指定到那些数据的URIs列表。MIME类型会根据分享的混合内容而不同。例如,如果分享3张JPEG的图片,那么MIME类型仍然是image/jpeg。如果是不同图片格式的话,应该是用image/* 来匹配那些可以接收任何图片类型的activity。如果需要分享多种不同类型的数据,可以使用*/*来表示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.."));
接收从其他App返回的数据
更新我们的manifest文件(Update Your Manifest)
Intent filters告诉Android系统一个程序愿意接受的数据类型。我们可以创建intent filters来表明程序能够接收的action类型。下面是个例子,对三个activit分别指定接受单张图片,文本与多张图片。<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>
当某个程序尝试通过创建一个intent并将其传递给startActivity来分享一些东西时,我们的程序会被呈现在一个列表中让用户进行选择。如果用户选择了我们的程序,相应的activity会被调用开启,这个时候就是我们如何处理获取到的数据的问题了。
处理接受到的数据(Handle the Incoming Content)
为了处理从Intent带来的数据,可以通过调用getIntent()方法来获取到Intent对象。拿到这个对象后,我们可以对其中面的数据进行判断,从而决定下一步行为。请记住,如果一个activity可以被其他的程序启动,我们需要在检查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线程里面去处理那些获取到的数据。
给ActionBar增加分享功能
更新菜单声明(Update Menu Declarations)
使用ShareActionProvider的第一步,在menu resources对应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>
这表明了该item的appearance与function需要与ShareActionProvider匹配。此外,你还需要告诉provider想分享的内容。
Set the Share Intent(设置分享的intent)
为了实现ShareActionProvider的功能,我们必须为它提供一个intent。带有ACTION_SEND和附加数据(例如EXTRA_TEXT与 EXTRA_STREAM)的。使用ShareActionProvider的例子如下: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); } }
相关文章推荐
- Android官方开发文档Training系列课程中文版:分享简单数据之发送简单数据给其它APP
- Android官方开发文档Training系列课程中文版:分享简单数据之从其它APP接收简单数据
- Android官方开发文档Training系列课程中文版:分享简单数据之添加简单的分享行为
- Android官方培训课程学习(四): 数据存储
- Android官方培训课程学习(三): 使用Fragment建立动态UI
- Android官方培训课程学习(五): 与其他应用交互
- Android官方培训课程学习(一): 兼容不同设备
- Android官方培训课程学习(二): 管理activity的生命周期
- 跟着Android官方培训课程学习,挖坑填坑记录
- Android官方培训课程中文版(v0.9.4)
- android学习记录3(数据存储的方式:sqlite、sp、存文件。listview简单使用)
- Android实战开发租赁管理软件(适配UI,数据的存储,多线程下载)课程分享
- Android学习之简单的数据存储
- Google Android官方培训课程中文版
- Google Android官方培训课程中文版
- Android官方培训课程中文版(v0.9.1)
- Android官方培训课程中文版
- Hadoop大数据零基础高端实战培训系列课程分享
- Android官方培训课程中文版
- Android官方培训课程中文版(v0.9.3)