Android应用中使用及实现系统“分享”接口

简介:

为了应用的推广、传播,很多的应用中都有“分享”功能,一个按钮,点击后会出现短信、微博等等一切实现了分享功能的应用列表。这一篇文章主要介绍怎么调用分享功能和怎么实现分享接口让自己应用出现分享列表中。Android应用中能很方便的完成这些功能,这也正是Android的伟大之处,他能很简单的完成应用之间的沟通以相互整合。


调用分享功能

1、分享文本

分享功能使用的隐式启动Activity的方法,这里的Action使用的是ACTION_SEND


  1. Intent sendIntent = new Intent();  

  2. sendIntent.setAction(Intent.ACTION_SEND);  

  3. sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");  

  4. sendIntent.setType("text/plain");  

  5. startActivity(sendIntent);  



效果如下图的图一。

2、改变分享列表标题

   使用上面的分享方式分享列表标题为“使用一下内容完成操作”,Android中提供了Intent.createChooser(),这样能一直显示分享选择列表,并且修改了分享列表标题内容。

  1. Intent sendIntent = new Intent();  

  2. sendIntent.setAction(Intent.ACTION_SEND);  

  3. sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");  

  4. sendIntent.setType("text/plain");  

  5. startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));  


使用Intent.createChooser()的好处:


If you callIntent.createChooser() for the intent, Android will always display the chooser. This has some advantages:

  • Even if the user has previously selected a default action for this intent, the chooser will still be displayed.

  • If no applications match, Android displays a system message.

  • You can specify a title for the chooser dialog.


SouthEastSouthEast


   分享功能不只是Intent.EXTRA_TEXT,还可以EXTRA_EMAILEXTRA_CCEXTRA_BCC,EXTRA_SUBJECT. 只需要接受方完成响应数据接受。


3、分享图片

   分享功能还支持二进制内容(Binary Content),但是多数是处理的图片,因为shareIntent.setType("image/jpeg")这一项设置了内容类型。可也以是其他类型,需要接受方支持。


  1. Intent shareIntent = new Intent();  

  2. shareIntent.setAction(Intent.ACTION_SEND);  

  3. shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);  

  4. shareIntent.setType("image/jpeg");  

  5. startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));  



4、分享图片列表

   分享功能不仅支持单张图片,还支持图片列表,这里还是说的范围太窄了,应该声明不仅仅是图片。


  1. ArrayList<Uri> imageUris = new ArrayList<Uri>();  

  2. imageUris.add(imageUri1); // Add your image URIs here

  3. imageUris.add(imageUri2);  

  4. Intent shareIntent = new Intent();  

  5. shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);  

  6. shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);  

  7. shareIntent.setType("image/*");  

  8. startActivity(Intent.createChooser(shareIntent, "Share images to.."));  




实现分享功能

   上面说的都是怎么调用分享功能,以下就开始写怎么实现分享功能,让我们的应用也出现在分享列表中。前面也说了分享功能是使用隐式调用Activtiy实现的,Activity需要声明<intent-filter> 。


声明intent-filter


  1. <activity  

  2.           android:name="com.example.sharedemo.ShareActivity"

  3.           android:label="@string/app_name" >  

  4.           <intent-filter>  

  5.               <action android:name="android.intent.action.SEND" />  

  6.               <category android:name="android.intent.category.DEFAULT" />  

  7.               <data android:mimeType="image/*" />  

  8.           </intent-filter>  

  9.           <intent-filter>  

  10.               <action android:name="android.intent.action.SEND" />  

  11.               <category android:name="android.intent.category.DEFAULT" />  

  12.               <data android:mimeType="text/plain" />  

  13.           </intent-filter>  

  14.           <intent-filter>  

  15.               <action android:name="android.intent.action.SEND_MULTIPLE" />  

  16.               <category android:name="android.intent.category.DEFAULT" />  

  17.               <data android:mimeType="image/*" />  

  18.           </intent-filter>  

  19.       </activity>  



上面声明了三种intent-filter,当然可以更多,这里只是举个例子,


处理接收数据

声明了intent-filter,响应的Activity就要处理响应的数据,示例如下:


  1. publicclass ShareActivity extends Activity{  

  2. @Override

  3. protectedvoid onCreate(Bundle savedInstanceState) {  

  4. // TODO Auto-generated method stub

  5. super.onCreate(savedInstanceState);  

  6. // Get intent, action and MIME type

  7.        Intent intent = getIntent();  

  8.        String action = intent.getAction();  

  9.        String type = intent.getType();  

  10. if (Intent.ACTION_SEND.equals(action) && type != null) {  

  11. if ("text/plain".equals(type)) {  

  12.                handleSendText(intent); // Handle text being sent

  13.            } elseif (type.startsWith("image/")) {  

  14.                handleSendImage(intent); // Handle single image being sent

  15.            }  

  16.        } elseif (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {  

  17. if (type.startsWith("image/")) {  

  18.                handleSendMultipleImages(intent); // Handle multiple images being sent

  19.            }  

  20.        } else {  

  21. // Handle other intents, such as being started from the home screen

  22.        }  

  23.    }  

  24. void handleSendText(Intent intent) {  

  25.        String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);  

  26.        String sharedTitle = intent.getStringExtra(Intent.EXTRA_TITLE);  

  27. if (sharedText != null) {  

  28. // Update UI to reflect text being shared

  29.        }  

  30.    }  

  31. void handleSendImage(Intent intent) {  

  32.        Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);  

  33. if (imageUri != null) {  

  34. // Update UI to reflect image being shared

  35.        }  

  36.    }  

  37. void handleSendMultipleImages(Intent intent) {  

  38.        ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);  

  39. if (imageUris != null) {  

  40. // Update UI to reflect multiple images being shared

  41.        }  

  42.    }  

  43. }  



通过声明intent-filter,处理接受到的数据就能完成分享的接收功能。


更多

   上面只做了分享功能简单的说明,伴随着Android api的升级,也出现了一些新的完成“分享”功能的方法,比如ShareActionProvider ,更多请参考。



示例下载


/**
* @author 张兴业
*  iOS入门群:83702688
*  android开发进阶群:241395671
*  我的新浪微博:@张兴业TBOW
*/


参考:

http://developer.android.com/training/sharing/index.html



     本文转自xyz_lmn51CTO博客,原文链接:http://blog.51cto.com/xyzlmn/1344472,如需转载请自行联系原作者

相关文章
|
3天前
|
机器学习/深度学习 Java Shell
[RK3568][Android12.0]--- 系统自带预置第三方APK方法
[RK3568][Android12.0]--- 系统自带预置第三方APK方法
17 0
|
1天前
|
Android开发
Android 如何将定制的Launcher成为系统中唯一的Launcher
Android 如何将定制的Launcher成为系统中唯一的Launcher
|
1天前
|
开发工具 Android开发 Windows
Android应用] 问题2:ERROR: unknown virtual device name:
Android应用] 问题2:ERROR: unknown virtual device name:
|
1天前
|
XML JSON API
转Android上基于JSON的数据交互应用
转Android上基于JSON的数据交互应用
|
1天前
|
Android开发
Android应用实例(一)之---有道辞典VZ.0
Android应用实例(一)之---有道辞典VZ.0
|
2天前
|
安全 Java Android开发
构建高效Android应用:采用Kotlin进行内存优化的策略
【5月更文挑战第8天】 在移动开发领域,性能优化一直是开发者关注的焦点。特别是对于Android应用而言,合理管理内存资源是确保应用流畅运行的关键因素之一。近年来,Kotlin作为官方推荐的开发语言,以其简洁、安全和互操作性的特点受到开发者青睐。本文将深入探讨利用Kotlin语言特性,通过具体策略对Android应用的内存使用进行优化,旨在帮助开发者提高应用性能,减少内存消耗,避免常见的内存泄漏问题。
6 0
|
3天前
|
Java Android开发
Android OTG U盘无法显示在系统文件管理的修改
Android OTG U盘无法显示在系统文件管理的修改
5 0
|
3天前
|
安全 Android开发
修改Android系统的签名
修改Android系统的签名
14 0
|
3天前
|
安全 Android开发
Android 系统签名
Android 系统签名
13 0
|
3天前
|
Android开发
Android APP 隐藏系统软键盘的方法
Android APP 隐藏系统软键盘的方法
10 0