Android微信SDK实现分享

用微信提供的SDK来实现分享:

http://open.weixin.qq.com下载Android相关的jar包,将libammsdk.jar加入到项目中。

微信分享的核心类,部分代码如下:

WechatShareManager.java

package com.jackie.umeng.share; 

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.Toast; 

import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXImageObject;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXTextObject;
import com.tencent.mm.sdk.modelmsg.WXVideoObject;
import com.tencent.mm.sdk.modelmsg.WXWebpageObject;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory; 

/**
 * 实现微信分享功能的核心类
 * @author chengcj1
 *
 */
public class WechatShareManager { 

 private static final int THUMB_SIZE = 150; 

 public static final int WECHAT_SHARE_WAY_TEXT = 1; //文字
 public static final int WECHAT_SHARE_WAY_PICTURE = 2; //图片
 public static final int WECHAT_SHARE_WAY_WEBPAGE = 3; //链接
 public static final int WECHAT_SHARE_WAY_VIDEO = 4; //视频
 public static final int WECHAT_SHARE_TYPE_TALK = SendMessageToWX.Req.WXSceneSession; //会话
 public static final int WECHAT_SHARE_TYPE_FRENDS = SendMessageToWX.Req.WXSceneTimeline; //朋友圈 

 private static WechatShareManager mInstance;
 private ShareContent mShareContentText, mShareContentPicture, mShareContentWebpag, mShareContentVideo;
 private IWXAPI mWXApi;
 private Context mContext; 

 private WechatShareManager(Context context){
  this.mContext = context;
  //初始化数据
  //初始化微信分享代码
  initWechatShare(context);
 } 

 /**
  * 获取WeixinShareManager实例
  * 非线程安全,请在UI线程中操作
  * @return
  */
 public static WechatShareManager getInstance(Context context){
  if(mInstance == null){
   mInstance = new WechatShareManager(context);
  }
  return mInstance;
 } 

 private void initWechatShare(Context context){
  if (mWXApi == null) {
   mWXApi = WXAPIFactory.createWXAPI(context, WechatShareUtil.WECHAT_APP_ID, true);
  }
  mWXApi.registerApp(WechatShareUtil.WECHAT_APP_ID);
 } 

 /**
  * 通过微信分享
  * @param shareWay 分享的方式(文本、图片、链接)
  * @param shareType 分享的类型(朋友圈,会话)
  */
 public void shareByWebchat(ShareContent shareContent, int shareType){
  switch (shareContent.getShareWay()) {
  case WECHAT_SHARE_WAY_TEXT:
   shareText(shareContent, shareType);
   break;
  case WECHAT_SHARE_WAY_PICTURE:
   sharePicture(shareContent, shareType);
   break;
  case WECHAT_SHARE_WAY_WEBPAGE:
   shareWebPage(shareContent, shareType);
   break;
  case WECHAT_SHARE_WAY_VIDEO:
   shareVideo(shareContent, shareType);
   break;
  }
 } 

 private abstract class ShareContent {
  protected abstract int getShareWay();
  protected abstract String getContent();
  protected abstract String getTitle();
  protected abstract String getURL();
  protected abstract int getPictureResource();
 } 

 /**
  * 设置分享文字的内容
  * @author chengcj1
  *
  */
 public class ShareContentText extends ShareContent {
  private String content; 

  /**
   * 构造分享文字类
   * @param text 分享的文字内容
   */
  public ShareContentText(String content){
   this.content = content;
  } 

  @Override
  protected int getShareWay() {
   return WECHAT_SHARE_WAY_TEXT;
  } 

  @Override
  protected String getContent() {
   return content;
  } 

  @Override
  protected String getTitle() {
   return null;
  } 

  @Override
  protected String getURL() {
   return null;
  } 

  @Override
  protected int getPictureResource() {
   return -1;
  }
 } 

 /*
  * 获取文本分享对象
  */
 public ShareContent getShareContentText(String content) {
  if (mShareContentText == null) {
   mShareContentText = new ShareContentText(content);
  }
  return (ShareContentText) mShareContentText;
 } 

 /**
  * 设置分享图片的内容
  * @author chengcj1
  *
  */
 public class ShareContentPicture extends ShareContent {
  private int pictureResource;
  public ShareContentPicture(int pictureResource){
   this.pictureResource = pictureResource;
  } 

  @Override
  protected int getShareWay() {
   return WECHAT_SHARE_WAY_PICTURE;
  } 

  @Override
  protected int getPictureResource() {
   return pictureResource;
  } 

  @Override
  protected String getContent() {
   return null;
  } 

  @Override
  protected String getTitle() {
   return null;
  } 

  @Override
  protected String getURL() {
   return null;
  }
 } 

 /*
  * 获取图片分享对象
  */
 public ShareContent getShareContentPicture(int pictureResource) {
  if (mShareContentPicture == null) {
   mShareContentPicture = new ShareContentPicture(pictureResource);
  }
  return (ShareContentPicture) mShareContentPicture;
 } 

 /**
  * 设置分享链接的内容
  * @author chengcj1
  *
  */
 public class ShareContentWebpage extends ShareContent {
  private String title;
  private String content;
  private String url;
  private int pictureResource;
  public ShareContentWebpage(String title, String content, String url, int pictureResource){
   this.title = title;
   this.content = content;
   this.url = url;
   this.pictureResource = pictureResource;
  } 

  @Override
  protected int getShareWay() {
   return WECHAT_SHARE_WAY_WEBPAGE;
  } 

  @Override
  protected String getContent() {
   return content;
  } 

  @Override
  protected String getTitle() {
   return title;
  } 

  @Override
  protected String getURL() {
   return url;
  } 

  @Override
  protected int getPictureResource() {
   return pictureResource;
  }
 } 

 /*
  * 获取网页分享对象
  */
 public ShareContent getShareContentWebpag(String title, String content, String url, int pictureResource) {
  if (mShareContentWebpag == null) {
   mShareContentWebpag = new ShareContentWebpage(title, content, url, pictureResource);
  }
  return (ShareContentWebpage) mShareContentWebpag;
 } 

 /**
  * 设置分享视频的内容
  * @author chengcj1
  *
  */
 public class ShareContentVideo extends ShareContent {
  private String url;
  public ShareContentVideo(String url) {
   this.url = url;
  } 

  @Override
  protected int getShareWay() {
   return WECHAT_SHARE_WAY_VIDEO;
  } 

  @Override
  protected String getContent() {
   return null;
  } 

  @Override
  protected String getTitle() {
   return null;
  } 

  @Override
  protected String getURL() {
   return url;
  } 

  @Override
  protected int getPictureResource() {
   return -1;
  }
 } 

 /*
  * 获取视频分享内容
  */
 public ShareContent getShareContentVideo(String url) {
  if (mShareContentVideo == null) {
   mShareContentVideo = new ShareContentVideo(url);
  }
  return (ShareContentVideo) mShareContentVideo;
 } 

 /*
  * 分享文字
  */
 private void shareText(ShareContent shareContent, int shareType) {
  String text = shareContent.getContent();
  //初始化一个WXTextObject对象
  WXTextObject textObj = new WXTextObject();
  textObj.text = text;
  //用WXTextObject对象初始化一个WXMediaMessage对象
  WXMediaMessage msg = new WXMediaMessage();
  msg.mediaObject = textObj;
  msg.description = text;
  //构造一个Req
  SendMessageToWX.Req req = new SendMessageToWX.Req();
  //transaction字段用于唯一标识一个请求
  req.transaction = buildTransaction("textshare");
  req.message = msg;
  //发送的目标场景, 可以选择发送到会话 WXSceneSession 或者朋友圈 WXSceneTimeline。 默认发送到会话。
  req.scene = shareType;
  mWXApi.sendReq(req);
 } 

 /*
  * 分享图片
  */
 private void sharePicture(ShareContent shareContent, int shareType) {
  Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), shareContent.getPictureResource());
  WXImageObject imgObj = new WXImageObject(bitmap); 

  WXMediaMessage msg = new WXMediaMessage();
  msg.mediaObject = imgObj; 

  Bitmap thumbBitmap = Bitmap.createScaledBitmap(bitmap, THUMB_SIZE, THUMB_SIZE, true);
  bitmap.recycle();
  msg.thumbData = Util.bmpToByteArray(thumbBitmap, true); //设置缩略图 

  SendMessageToWX.Req req = new SendMessageToWX.Req();
  req.transaction = buildTransaction("imgshareappdata");
  req.message = msg;
  req.scene = shareType;
  mWXApi.sendReq(req);
 } 

 /*
  * 分享链接
  */
 private void shareWebPage(ShareContent shareContent, int shareType) {
  WXWebpageObject webpage = new WXWebpageObject();
  webpage.webpageUrl = shareContent.getURL();
  WXMediaMessage msg = new WXMediaMessage(webpage);
  msg.title = shareContent.getTitle();
  msg.description = shareContent.getContent(); 

  Bitmap thumb = BitmapFactory.decodeResource(mContext.getResources(), shareContent.getPictureResource());
  if(thumb == null) {
   Toast.makeText(mContext, "图片不能为空", Toast.LENGTH_SHORT).show();
  } else {
   msg.thumbData = Util.bmpToByteArray(thumb, true);
  } 

  SendMessageToWX.Req req = new SendMessageToWX.Req();
  req.transaction = buildTransaction("webpage");
  req.message = msg;
  req.scene = shareType;
  mWXApi.sendReq(req);
 } 

 /*
  * 分享视频
  */
 private void shareVideo(ShareContent shareContent, int shareType) {
  WXVideoObject video = new WXVideoObject();
  video.videoUrl = shareContent.getURL(); 

  WXMediaMessage msg = new WXMediaMessage(video);
  msg.title = shareContent.getTitle();
  msg.description = shareContent.getContent();
  Bitmap thumb = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.send_music_thumb);
//  BitmapFactory.decodeStream(new URL(video.videoUrl).openStream());
  /**
   * 测试过程中会出现这种情况,会有个别手机会出现调不起微信客户端的情况。造成这种情况的原因是微信对缩略图的大小、title、description等参数的大小做了限制,所以有可能是大小超过了默认的范围。
   * 一般情况下缩略图超出比较常见。Title、description都是文本,一般不会超过。
   */
  Bitmap thumbBitmap = Bitmap.createScaledBitmap(thumb, THUMB_SIZE, THUMB_SIZE, true);
  thumb.recycle();
  msg.thumbData = Util.bmpToByteArray(thumbBitmap, true); 

  SendMessageToWX.Req req = new SendMessageToWX.Req();
  req.transaction = buildTransaction("video");
  req.message = msg;
  req.scene = shareType;
  mWXApi.sendReq(req);
 } 

 private String buildTransaction(final String type) {
  return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
 }
}

MainActivity.java

package com.jackie.umeng.share; 

import com.jackie.umeng.share.WechatShareManager.ShareContentPicture;
import com.jackie.umeng.share.WechatShareManager.ShareContentText;
import com.jackie.umeng.share.WechatShareManager.ShareContentVideo; 

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; 

public class MainActivity extends Activity implements OnClickListener {
 private Button mShareText, mSharePicture, mShareVideo;
 private WechatShareManager mShareManager; 

 private Context mContext; 

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main); 

  mShareText = (Button) findViewById(R.id.share_text);
  mSharePicture = (Button) findViewById(R.id.share_picture);
  mShareVideo = (Button) findViewById(R.id.share_video);
  mShareText.setOnClickListener(this);
  mSharePicture.setOnClickListener(this);
  mShareVideo.setOnClickListener(this); 

  mContext = this; 

  mShareManager = WechatShareManager.getInstance(mContext);
 } 

 @Override
 public void onClick(View v) {
  if (!isWebchatAvaliable()) {
   Toast.makeText(mContext, "请先安装微信", Toast.LENGTH_LONG).show();
   return;
  }
  switch (v.getId()) {
  case R.id.share_text:
   ShareContentText mShareContentText = (ShareContentText) mShareManager.getShareContentText("微信文本分享");
   mShareManager.shareByWebchat(mShareContentText, WechatShareManager.WECHAT_SHARE_TYPE_FRENDS);
   break;
  case R.id.share_picture:
   ShareContentPicture mShareContentPicture = (ShareContentPicture) mShareManager.getShareContentPicture(R.drawable.share);
   mShareManager.shareByWebchat(mShareContentPicture, WechatShareManager.WECHAT_SHARE_TYPE_FRENDS);
   break;
  case R.id.share_video:
   ShareContentVideo mShareContentVideo = (ShareContentVideo) mShareManager.getShareContentVideo("http://baidu.hz.letv.com/kan/agSlT?fr=v.baidu.com/");
   mShareManager.shareByWebchat(mShareContentVideo, WechatShareManager.WECHAT_SHARE_TYPE_FRENDS);
   break;
  default:
   break;
  }
 } 

 private boolean isWebchatAvaliable() {
  //检测手机上是否安装了微信
  try {
   getPackageManager().getPackageInfo("com.tencent.mm", PackageManager.GET_ACTIVITIES);
   return true;
  } catch (Exception e) {
   return false;
  }
 }
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • javascript实现微信分享

    代码很简单,我这里就不多废话了,直接奉上源码: 复制代码 代码如下: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServ

  • android 微信 sdk api调用不成功解决方案

    最近一直在调用微信的API,却发现一直调用不成功,纠结了好久,各方面找教程,找官方,官方里的文档也只是写得很模糊,说是按三步走. 1.申请App_ID 2.填写包名3. 获取程序签名的md5值, 这三步只要你走对了就能调通,可是大家都不知道有时候我们打包的keystore和我们打包的keystore获取到的程序签名的md5是不一样的.我们在申请的时候填的程序签名值是正式打包的,但我们在eclipse部署上去的却是用的我们默认的debug.keystore.而这样导致的后果就是程序签名不一样,会一

  • Android开发:微信授权登录与微信分享完全解析

    前言 在移动互联网浪潮中,联网APP已经把单机拍死在沙滩上,很多公司都希望自家应用能够有一套帐号系统,可是许多用户却并不一定买账:我凭啥注册你家应用的帐号?微博,微信,QQ几乎成了每个人手机中的必装应用,于是微信,微博,QQ说了:来来来,你们都可以用我家的帐号登录你家应用,只要你遵循OAuth2.0协议标准就行.于是第三方社交帐号登陆成为了许多新兴应用的选择,由于腾讯官方微信开放平台的在线文档相对最新的SDK有些出入,并且登录相关的文档结构次序有些紊乱,今天就把我的一些经验记录在此,对微信开放平

  • Android应用中加入微信分享简单方法

    一.申请你的AppID http://open.weixin.qq.com/ 友情提示:推荐使用eclipse打包软件最后一步的MD5值去申请AppID 二.官网下载libammsdk.jar包 http://open.weixin.qq.com/download/?lang=zh_CN 三.将libammsdk.jar复制到工程的libs目录 四.在需要分享的Activity编写代码 复制代码 代码如下: private IWXAPI wxApi;  //实例化  wxApi = WXAPIF

  • js实现通用的微信分享组件示例

    一.可定义的信息 1.分享时显示的LOGO:2.分享LOGO的宽度:3.分享LOGO的高度:4.分享出去显示的标题(默认调用网页标题):5.分享出去显示的描述(默认调用网页标题):6.分享链接(默认为当前页面的URL).7.分享微信的APPID(一般为空). 二.使用方法1.引入微信分享组件js: 复制代码 代码如下: /******************************* * Author:Mr.Think * Description:微信分享通用代码 * 使用方法:_WXShare

  • Android—基于微信开放平台v3SDK开发(微信支付填坑)

    接触微信支付之前听说过这是一个坑,,,心里已经有了准备...我以为我没准跳坑出不来了,没有想到我填上了,调用成功之后我感觉公司所有的同事都是漂亮的,隔着北京的大雾霾我仿佛看见了太阳~~~好了,装逼结束...进入正题 开发准备: 1.在微信开放平台申请账号 2.成功后创建应用,就是填一些看似很官方很正经的资料了...(说审核7天左右,没有意外的情况下你的app第二天就审核成功了是不是很开心,有了appid,是不是就可以调用微   信支付了????-------想多了,真的) 3.微信支付是需要额外

  • Android编程实现微信分享信息的方法

    本文实例讲述了Android编程实现微信分享信息的方法.分享给大家供大家参考,具体如下: 随着微信越来越火,越来越多的应用要求有分享到微信的功能.虽然有很多平台都帮集成有分享功能,比如友盟.但是个人觉得友盟集成的东西太多了,自己封装得太过分了,很多资源文件也要带进去,所以感觉不是怎么好,所以自己也研究了一下微信的调用其SDK分享.下面说说步骤. 第一:下载官方的sdk demo. 下载地址:http://open.weixin.qq.com/download/?lang=zh_CN 第二:解压,

  • 记录Android微信分享功能的吐槽与思考

    在App内潜入分享到微信好友或朋友圈的功能想必大家已经屡见不鲜了,比如Android分享一个网页信息(URL)到微信客户端的代码: /** * 微信分享:分享网页 * @param context * @param url * @param title * @param description * @param scene */ public static void shareToWeChatWithWebpage(Context context, String url, String titl

  • javascript实现修改微信分享的标题内容等

    代码超级简单,这里就不多废话了,小伙伴们自己看注释吧, 奉上代码: 复制代码 代码如下: <script> //微信分享 var wimg = "分享图片网址123"; var wurl = "分享网址123"; var wdesc = '分享内容123'; var wtit = '分享标题123'; var wappid = '';   function shareMsg() {//<span style="font-family: A

  • Android编程之微信SDK分享功能过程步骤详细分析

    本文实例讲述了Android编程之微信SDK分享功能过程步骤详细分析.分享给大家供大家参考,具体如下: 之前已经分析过怎么用官方的demo分享微信信息了,在这里我就不再多说,其中关于在自己应用分享说得很简单,本文作者也是经过一番折腾才弄成功,为了以后让大家都少走弯路,决定在这里从头到尾介绍怎么在自己的应用中分享功能 注意:顺序不能乱!! 1.建立自己的应用 TestShareWX (1)应用包名是com.freeson.test,然后建立一个测试Activity,名字为TestActivity,

随机推荐