欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android微信SDK实现分享

程序员文章站 2024-02-28 09:55:34
用微信提供的sdk来实现分享: 从下载android相关的jar包,将libammsdk.jar加入到项目中。 微信分享的核心类,部分代码如下: wechatshar...

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

从下载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; 
  } 
 } 
} 

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