为了应用的推广、传播,很多的应用中都有“分享”功能,一个按钮,点击后会出现短信、微博等等一切实现了分享功能的应用列表。这一篇文章主要介绍怎么调用分享功能和怎么实现分享接口让自己应用出现分享列表中。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.

                    

分享功能不只是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. public class ShareActivity extends Activity{
  2. @Override
  3. protected void 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. } else if (type.startsWith("image/")) {
  14. handleSendImage(intent); // Handle single image being sent
  15. }
  16. } else if (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

Android应用中使用及实现系统“分享”接口的更多相关文章

  1. Android应用中实现系统“分享”接口

    在android下各种文件管理器中,我们选择一个文件,点击分享可以看到弹出一些app供我们选择,这个是android系统分享功能,我们做的app也可以出现在这个列表中. 第一步:在Manifest.x ...

  2. 在Android Studio中使用shareSDK进行社会化分享(图文教程)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  3. Android Intent和IntentFilter详解与使用及实现系统“分享”接口

    Intent Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描述,负责找到 ...

  4. Android开发中Parcelable接口的使用方法

    在网上看到很多Android初入门的童鞋都在问Parcelable接口的使用方法,小编参考了相关Android教程,看到里面介绍的序列化方法主要有两种分别是实现Serializable接口和实现Par ...

  5. android - 调用系统分享功能分享图片

    step1: 编写分享代码, 将Uri的生成方式改为由FileProvider提供的临时授权路径,并且在intent中添加flag 注意:在Android7.0之后,调用系统分享,传入URI的时候可能 ...

  6. 我的Android进阶之旅------>如何获取系统中定义了那些权限

    在Window控制台中输入如下命令可以看到Android系统中列出的所有权限(如果自定义权限注册成功,在这里也会找到这些自定义的权限) adb shell pm list permissions C: ...

  7. android开发中系统自带语音模块的使用

    android开发中系统自带语音模块的使用需求:项目中需要添加语音搜索模块,增加用户体验解决过程:在网上搜到语音搜索例子,参考网上代码,加入到了自己的项目,完成产品要求.这个问题很好解决,网上能找到很 ...

  8. Android开发中怎样调用系统Email发送邮件(多种调用方式)

    在Android中调用其他程序进行相关处理,几乎都是使用的Intent,所以,Email也不例外,所谓的调用Email,只是说Email可以接收Intent并做这些事情 我们都知道,在Android中 ...

  9. unity3d开发的android应用中增加AD系统的详细步骤

    unity3d开发的android应用中增加AD系统的详细步骤 博客分类: Unity3d unity3d  Unity3d已经支持android,怎样在程序里增加admob?  试了一下,确实能够, ...

随机推荐

  1. avalon实现一个简单的带增删改查的成绩单

    自从angular问世,一直就有去了解学习angular,一直想用angular去做一个项目,但无奈,大ng是国外产物,ng1.2版本就只兼容到IE8,1.3后的几个版本提升到IE9,据说NG2.0更 ...

  2. UC脱茧蜕变,移动资讯市场格局再生变

    日前,UC浏览器正式更名为UC,同时正式发布大数据驱动的独立资讯应用“UC头条”.而整个UC品牌也从工具类升级为优质资讯内容平台,并吹响了向“大数据新型媒体平台”进军的冲锋号.根据UC官方公布的数据显 ...

  3. java集合类总结二

    上篇已经总结了常用集合类的一些基本特征以及他们之间的区别,下面,再对集合类部分进行总结 一.集合类的常用方法 1.remove方法:移除元素操作,下面以ArrayList为例. import java ...

  4. 关于C++单件模式释放对象

    http://blog.csdn.net/windboyzsj/article/details/2790485 最近接触的一个项目要用到单件模式,我像往常一样哒哒(敲击键盘ing)一个单件模式的典型结 ...

  5. Win7下SQLite的简单使用

    前言 SQLite 是一个软件库,实现了自给自足的.无服务器的.零配置的.事务性的 SQL 数据库引擎.SQLite 是在世界上最广泛部署的 SQL 数据库引擎.SQLite 源代码不受版权限制. 简 ...

  6. libuv在cocos2d-x中的使用

    libuv经过Node.js的实践和应用,已经证明非常之成熟,本来之前项目用的是这个:clsocket https://github.com/DFHack/clsocket  当初选它的主要原因是它支 ...

  7. 了解Browserify

    Browserify是一个Javascript的库,可以用来把多个Module打包到一个文件中,并且能很好地应对Modules之间的依赖关系.而Module是封装了属性和功能的单元,是一个Javasc ...

  8. 简单谈谈Resource,Drawable和Bitmap之间的转换

    一直接触这些东西,还是归个类整理一下比较好. Resource -> Drawable Drawable draw1 = this.getResources().getDrawable(R.dr ...

  9. MDT部署中命令行脚本的使用。

    参考:http://blogs.technet.com/b/deploymentguys/archive/2010/07/07/using-command-shell-scripts-with-mdt ...

  10. ELK——在 CentOS/Linux 把 Kibana 3.0 部署在 Nginx 1.9.12

    上一篇文章<安装 logstash 2.2.0.elasticsearch 2.2.0 和 Kibana 3.0>,介绍了如何安装 Logstash.Elasticsearch 以及用 P ...