非常多的应用为了应用的推广和传播都会使用“分享”的功能,点击分享button。就能将想要分享的内容或者图片分享至QQ空间、微博、微信朋友圈等实现了分享功能的应用。这篇文章主要是为了学习与探索调用系统实现分享功能或者直接调起实现了分享功能的应用的activity来进行分享。
眼下实现一键分享功能的方式有两种:
1.须要集成第三方官方SDK包,在获得官方授权后调用其API来完毕一键分享功能,比如使用友盟分享等
长处:无缝集成。功能多
缺点:须要集成官方的SDK包并通过申请官方的授权才可进行开发
2.不须要使用不论什么第三方SDK包,能够直接调起实现了分享功能的应用的activity来进行分享
长处:不须要使用不论什么第三方SDK包和申请官方授权
缺点:须要手机安装你须要分享的应用(这一点非常重要,一開始測试的时候一直不成功,提示“没有应用可运行此操作”。后来找了非常久才发现是我手机没有安装相相应的应用。这也是不好方便的地方)
依照惯例先来看一下终于效果图:
ShareActivity.class
package com.xiaolijuan.sharedome; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.widget.RelativeLayout; import java.io.File; import java.util.ArrayList; /** * 项目名称:ShareDome * 类描写叙述: * 创建人:xiaolijuan * 创建时间:2016/1/13 23:48 */ public class ShareActivity extends Activity implements View.OnClickListener { private RelativeLayout mRlShareText, mRlShareSingleimage, mRlShareMultipleimage, mRlShareQQ, mRlShareTencent, mRlShareWechat, mRlShareMicroblog, mRlShareOther; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_share); bindView(); } private void bindView() { mRlShareText = (RelativeLayout) findViewById(R.id.rl_share_text); mRlShareSingleimage = (RelativeLayout) findViewById(R.id.rl_share_singleimage); mRlShareMultipleimage = (RelativeLayout) findViewById(R.id.rl_share_multipleimage); mRlShareQQ = (RelativeLayout) findViewById(R.id.rl_share_qq); mRlShareTencent = (RelativeLayout) findViewById(R.id.rl_share_qqtencent); mRlShareWechat = (RelativeLayout) findViewById(R.id.rl_share_wechat); mRlShareMicroblog = (RelativeLayout) findViewById(R.id.rl_share_microblog); mRlShareOther = (RelativeLayout) findViewById(R.id.rl_share_other); mRlShareText.setOnClickListener(new ShareText()); mRlShareSingleimage.setOnClickListener(new ShareSingleImage()); mRlShareMultipleimage.setOnClickListener(new ShareMultipleImage()); mRlShareQQ.setOnClickListener(this); mRlShareTencent.setOnClickListener(this); mRlShareWechat.setOnClickListener(this); mRlShareMicroblog.setOnClickListener(this); mRlShareOther.setOnClickListener(this); } //分享文字至全部第三方软件 public class ShareText implements View.OnClickListener { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_TEXT, "这里是分享内容"); intent.setType("text/plain"); //设置分享列表的标题。而且每次都显示分享列表 startActivity(Intent.createChooser(intent, "分享到")); } } //分享单张图片至全部第三方软件 public class ShareSingleImage implements View.OnClickListener { @Override public void onClick(View v) { String imagePath = Environment.getExternalStorageDirectory() + File.separator + "13e277bb0b9c2e3ab90229463357bf40.jpg"; //由文件得到uri Uri imageUri = Uri.fromFile(new File(imagePath)); Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri); shareIntent.setType("image/*"); startActivity(Intent.createChooser(shareIntent, "分享到")); } } //分享多张图片至全部第三方软件 public class ShareMultipleImage implements View.OnClickListener { @Override public void onClick(View v) { ArrayList<Uri> uriList = new ArrayList<>(); String path = Environment.getExternalStorageDirectory() + File.separator; uriList.add(Uri.fromFile(new File(path+"13e277bb0b9c2e3ab90229463357bf40.jpg"))); uriList.add(Uri.fromFile(new File(path+"869895e73ddd710e8a132afb37461bf0.jpg"))); uriList.add(Uri.fromFile(new File(path+"4753fc4cd47aa1833c70df4c08f4b7fa.jpg"))); uriList.add(Uri.fromFile(new File(path+"355ee87cf0ff612331a790f31b3ad113.jpg"))); uriList.add(Uri.fromFile(new File(path+"ce61ad4d44e3099d87040f38faabbf56.jpg"))); Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE); shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList); shareIntent.setType("image/*"); startActivity(Intent.createChooser(shareIntent, "分享到")); } } @Override public void onClick(View v) { String pakName = ""; Intent intent = new Intent(Intent.ACTION_SEND); // 启动分享发送的属性 intent.setType("text/plain"); // 分享发送的数据类型 switch (v.getId()) { case R.id.rl_share_qq: pakName = "com.qzone"; //qq空间 break; case R.id.rl_share_qqtencent: pakName = "com.tencent.WBlog"; //腾讯微博 break; case R.id.rl_share_wechat: pakName = "com.tencent.mm"; //微信 break; case R.id.rl_share_microblog: pakName = "com.sina.weibo"; //微博 break; case R.id.rl_share_other: break; default: break; } intent.setPackage(pakName); intent.putExtra(Intent.EXTRA_TEXT, "这里是分享内容"); // 分享的内容 this.startActivity(Intent.createChooser(intent, ""));// 目标应用选择对话框的标题; } }
因为分享功能是使用隐式调用Activtiy实现的,则需在响应的Activity中声明intent-filter。在相应的activity的xml里加上
<intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter>