Android微信SDK实现分享

来源:互联网 发布:asp.net 明细数据输入 编辑:程序博客网 时间:2024/05/18 01:20

摘自:http://blog.csdn.net/shineflowers/article/details/47951965

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

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

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

WechatShareManager.Java

[java] view plain copy
  1. package com.jackie.umeng.share;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.BitmapFactory;  
  6. import android.util.Log;  
  7. import android.widget.Toast;  
  8.   
  9. import com.tencent.mm.sdk.modelmsg.SendMessageToWX;  
  10. import com.tencent.mm.sdk.modelmsg.WXImageObject;  
  11. import com.tencent.mm.sdk.modelmsg.WXMediaMessage;  
  12. import com.tencent.mm.sdk.modelmsg.WXTextObject;  
  13. import com.tencent.mm.sdk.modelmsg.WXVideoObject;  
  14. import com.tencent.mm.sdk.modelmsg.WXWebpageObject;  
  15. import com.tencent.mm.sdk.openapi.IWXAPI;  
  16. import com.tencent.mm.sdk.openapi.WXAPIFactory;  
  17.    
  18. /** 
  19.  * 实现微信分享功能的核心类 
  20.  * @author chengcj1 
  21.  * 
  22.  */  
  23. public class WechatShareManager {  
  24.        
  25.     private static final int THUMB_SIZE = 150;  
  26.       
  27.     public static final int WECHAT_SHARE_WAY_TEXT = 1;   //文字  
  28.     public static final int WECHAT_SHARE_WAY_PICTURE = 2//图片  
  29.     public static final int WECHAT_SHARE_WAY_WEBPAGE = 3;  //链接  
  30.     public static final int WECHAT_SHARE_WAY_VIDEO = 4//视频  
  31.     public static final int WECHAT_SHARE_TYPE_TALK = SendMessageToWX.Req.WXSceneSession;  //会话  
  32.     public static final int WECHAT_SHARE_TYPE_FRENDS = SendMessageToWX.Req.WXSceneTimeline; //朋友圈  
  33.       
  34.     private static WechatShareManager mInstance;  
  35.     private ShareContent mShareContentText, mShareContentPicture, mShareContentWebpag, mShareContentVideo;  
  36.     private IWXAPI mWXApi;  
  37.     private Context mContext;  
  38.        
  39.     private WechatShareManager(Context context){  
  40.         this.mContext = context;  
  41.         //初始化数据  
  42.         //初始化微信分享代码  
  43.         initWechatShare(context);  
  44.     }  
  45.        
  46.     /** 
  47.      * 获取WeixinShareManager实例 
  48.      * 非线程安全,请在UI线程中操作 
  49.      * @return 
  50.      */  
  51.     public static WechatShareManager getInstance(Context context){  
  52.         if(mInstance == null){  
  53.             mInstance = new WechatShareManager(context);  
  54.         }  
  55.         return mInstance;  
  56.     }  
  57.        
  58.     private void initWechatShare(Context context){  
  59.         if (mWXApi == null) {  
  60.             mWXApi = WXAPIFactory.createWXAPI(context, WechatShareUtil.WECHAT_APP_ID, true);  
  61.         }  
  62.         mWXApi.registerApp(WechatShareUtil.WECHAT_APP_ID);  
  63.     }  
  64.        
  65.     /** 
  66.      * 通过微信分享 
  67.      * @param shareWay 分享的方式(文本、图片、链接) 
  68.      * @param shareType 分享的类型(朋友圈,会话) 
  69.      */  
  70.     public void shareByWebchat(ShareContent shareContent, int shareType){  
  71.         switch (shareContent.getShareWay()) {  
  72.         case WECHAT_SHARE_WAY_TEXT:  
  73.             shareText(shareContent, shareType);  
  74.             break;  
  75.         case WECHAT_SHARE_WAY_PICTURE:  
  76.             sharePicture(shareContent, shareType);  
  77.             break;  
  78.         case WECHAT_SHARE_WAY_WEBPAGE:  
  79.             shareWebPage(shareContent, shareType);  
  80.             break;  
  81.         case WECHAT_SHARE_WAY_VIDEO:  
  82.             shareVideo(shareContent, shareType);  
  83.             break;  
  84.         }  
  85.     }  
  86.        
  87.     private abstract class ShareContent {  
  88.         protected abstract int getShareWay();  
  89.         protected abstract String getContent();  
  90.         protected abstract String getTitle();  
  91.         protected abstract String getURL();  
  92.         protected abstract int getPictureResource();  
  93.     }  
  94.        
  95.     /** 
  96.      * 设置分享文字的内容 
  97.      * @author chengcj1 
  98.      * 
  99.      */  
  100.     public class ShareContentText extends ShareContent {  
  101.         private String content;  
  102.            
  103.         /** 
  104.          * 构造分享文字类 
  105.          * @param text 分享的文字内容 
  106.          */  
  107.         public ShareContentText(String content){  
  108.             this.content = content;  
  109.         }  
  110.           
  111.         @Override  
  112.         protected int getShareWay() {  
  113.             return WECHAT_SHARE_WAY_TEXT;  
  114.         }  
  115.    
  116.         @Override  
  117.         protected String getContent() {  
  118.             return content;  
  119.         }  
  120.    
  121.         @Override  
  122.         protected String getTitle() {  
  123.             return null;  
  124.         }  
  125.    
  126.         @Override  
  127.         protected String getURL() {  
  128.             return null;  
  129.         }  
  130.    
  131.         @Override  
  132.         protected int getPictureResource() {  
  133.             return -1;  
  134.         }  
  135.     }  
  136.       
  137.     /* 
  138.      * 获取文本分享对象 
  139.      */  
  140.     public ShareContent getShareContentText(String content) {  
  141.         if (mShareContentText == null) {  
  142.             mShareContentText = new ShareContentText(content);  
  143.         }  
  144.         return (ShareContentText) mShareContentText;  
  145.     }  
  146.        
  147.     /** 
  148.      * 设置分享图片的内容 
  149.      * @author chengcj1 
  150.      * 
  151.      */  
  152.     public class ShareContentPicture extends ShareContent {  
  153.         private int pictureResource;  
  154.         public ShareContentPicture(int pictureResource){  
  155.             this.pictureResource = pictureResource;  
  156.         }  
  157.           
  158.         @Override  
  159.         protected int getShareWay() {  
  160.             return WECHAT_SHARE_WAY_PICTURE;  
  161.         }  
  162.           
  163.         @Override  
  164.         protected int getPictureResource() {  
  165.             return pictureResource;  
  166.         }  
  167.            
  168.         @Override  
  169.         protected String getContent() {  
  170.             return null;  
  171.         }  
  172.    
  173.         @Override  
  174.         protected String getTitle() {  
  175.             return null;  
  176.         }  
  177.    
  178.         @Override  
  179.         protected String getURL() {  
  180.             return null;  
  181.         }  
  182.     }  
  183.       
  184.     /* 
  185.      * 获取图片分享对象 
  186.      */  
  187.     public ShareContent getShareContentPicture(int pictureResource) {  
  188.         if (mShareContentPicture == null) {  
  189.             mShareContentPicture = new ShareContentPicture(pictureResource);  
  190.         }  
  191.         return (ShareContentPicture) mShareContentPicture;  
  192.     }  
  193.        
  194.     /** 
  195.      * 设置分享链接的内容 
  196.      * @author chengcj1 
  197.      * 
  198.      */  
  199.     public class ShareContentWebpage extends ShareContent {  
  200.         private String title;  
  201.         private String content;  
  202.         private String url;  
  203.         private int pictureResource;  
  204.         public ShareContentWebpage(String title, String content, String url, int pictureResource){  
  205.             this.title = title;  
  206.             this.content = content;  
  207.             this.url = url;  
  208.             this.pictureResource = pictureResource;  
  209.         }  
  210.           
  211.         @Override  
  212.         protected int getShareWay() {  
  213.             return WECHAT_SHARE_WAY_WEBPAGE;  
  214.         }  
  215.    
  216.         @Override  
  217.         protected String getContent() {  
  218.             return content;  
  219.         }  
  220.    
  221.         @Override  
  222.         protected String getTitle() {  
  223.             return title;  
  224.         }  
  225.    
  226.         @Override  
  227.         protected String getURL() {  
  228.             return url;  
  229.         }  
  230.    
  231.         @Override  
  232.         protected int getPictureResource() {  
  233.             return pictureResource;  
  234.         }  
  235.     }  
  236.       
  237.     /* 
  238.      * 获取网页分享对象 
  239.      */  
  240.     public ShareContent getShareContentWebpag(String title, String content, String url, int pictureResource) {  
  241.         if (mShareContentWebpag == null) {  
  242.             mShareContentWebpag = new ShareContentWebpage(title, content, url, pictureResource);  
  243.         }  
  244.         return (ShareContentWebpage) mShareContentWebpag;  
  245.     }  
  246.       
  247.     /** 
  248.      * 设置分享视频的内容 
  249.      * @author chengcj1 
  250.      * 
  251.      */  
  252.     public class ShareContentVideo extends ShareContent {  
  253.         private String url;  
  254.         public ShareContentVideo(String url) {  
  255.             this.url = url;  
  256.         }  
  257.   
  258.         @Override  
  259.         protected int getShareWay() {  
  260.             return WECHAT_SHARE_WAY_VIDEO;  
  261.         }  
  262.   
  263.         @Override  
  264.         protected String getContent() {  
  265.             return null;  
  266.         }  
  267.   
  268.         @Override  
  269.         protected String getTitle() {  
  270.             return null;  
  271.         }  
  272.   
  273.         @Override  
  274.         protected String getURL() {  
  275.             return url;  
  276.         }  
  277.   
  278.         @Override  
  279.         protected int getPictureResource() {  
  280.             return -1;  
  281.         }  
  282.     }  
  283.       
  284.     /* 
  285.      * 获取视频分享内容 
  286.      */  
  287.     public ShareContent getShareContentVideo(String url) {  
  288.         if (mShareContentVideo == null) {  
  289.             mShareContentVideo = new ShareContentVideo(url);  
  290.         }  
  291.         return (ShareContentVideo) mShareContentVideo;  
  292.     }   
  293.        
  294.     /* 
  295.      * 分享文字 
  296.      */  
  297.     private void shareText(ShareContent shareContent, int shareType) {  
  298.         String text = shareContent.getContent();  
  299.         //初始化一个WXTextObject对象  
  300.         WXTextObject textObj = new WXTextObject();  
  301.         textObj.text = text;  
  302.         //用WXTextObject对象初始化一个WXMediaMessage对象  
  303.         WXMediaMessage msg = new WXMediaMessage();  
  304.         msg.mediaObject = textObj;  
  305.         msg.description = text;  
  306.         //构造一个Req  
  307.         SendMessageToWX.Req req = new SendMessageToWX.Req();  
  308.         //transaction字段用于唯一标识一个请求  
  309.         req.transaction = buildTransaction("textshare");  
  310.         req.message = msg;  
  311.         //发送的目标场景, 可以选择发送到会话 WXSceneSession 或者朋友圈 WXSceneTimeline。 默认发送到会话。  
  312.         req.scene = shareType;  
  313.         mWXApi.sendReq(req);  
  314.     }  
  315.    
  316.     /* 
  317.      * 分享图片 
  318.      */  
  319.     private void sharePicture(ShareContent shareContent, int shareType) {  
  320.         Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), shareContent.getPictureResource());  
  321.         WXImageObject imgObj = new WXImageObject(bitmap);  
  322.            
  323.         WXMediaMessage msg = new WXMediaMessage();  
  324.         msg.mediaObject = imgObj;  
  325.            
  326.         Bitmap thumbBitmap =  Bitmap.createScaledBitmap(bitmap, THUMB_SIZE, THUMB_SIZE, true);  
  327.         bitmap.recycle();  
  328.         msg.thumbData = WechatShareUtil.bitmapToByteArray(thumbBitmap, true);  //设置缩略图  
  329.            
  330.         SendMessageToWX.Req req = new SendMessageToWX.Req();  
  331.         req.transaction = buildTransaction("imgshareappdata");  
  332.         req.message = msg;  
  333.         req.scene = shareType;  
  334.         mWXApi.sendReq(req);  
  335.     }  
  336.    
  337.     /* 
  338.      * 分享链接 
  339.      */  
  340.     private void shareWebPage(ShareContent shareContent, int shareType) {  
  341.         WXWebpageObject webpage = new WXWebpageObject();  
  342.         webpage.webpageUrl = shareContent.getURL();  
  343.         WXMediaMessage msg = new WXMediaMessage(webpage);  
  344.         msg.title = shareContent.getTitle();  
  345.         msg.description = shareContent.getContent();  
  346.            
  347.         Bitmap thumb = BitmapFactory.decodeResource(mContext.getResources(), shareContent.getPictureResource());  
  348.         if(thumb == null) {  
  349.             Toast.makeText(mContext, "图片不能为空", Toast.LENGTH_SHORT).show();  
  350.         } else {  
  351.             msg.thumbData = WechatShareUtil.bitmapToByteArray(thumb, true);  
  352.         }  
  353.            
  354.         SendMessageToWX.Req req = new SendMessageToWX.Req();  
  355.         req.transaction = buildTransaction("webpage");  
  356.         req.message = msg;  
  357.         req.scene = shareType;  
  358.         mWXApi.sendReq(req);  
  359.     }  
  360.       
  361.     /* 
  362.      * 分享视频 
  363.      */  
  364.     private void shareVideo(ShareContent shareContent, int shareType) {  
  365.         WXVideoObject video = new WXVideoObject();  
  366.         video.videoUrl = shareContent.getURL();  
  367.   
  368.         WXMediaMessage msg = new WXMediaMessage(video);  
  369.         msg.title = shareContent.getTitle();  
  370.         msg.description = shareContent.getContent();  
  371.         Bitmap thumb = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.send_music_thumb);  
  372. //      BitmapFactory.decodeStream(new URL(video.videoUrl).openStream());  
  373.         /** 
  374.          * 测试过程中会出现这种情况,会有个别手机会出现调不起微信客户端的情况。造成这种情况的原因是微信对缩略图的大小、title、description等参数的大小做了限制,所以有可能是大小超过了默认的范围。 
  375.          * 一般情况下缩略图超出比较常见。Title、description都是文本,一般不会超过。 
  376.          */  
  377.         Bitmap thumbBitmap =  Bitmap.createScaledBitmap(thumb, THUMB_SIZE, THUMB_SIZE, true);  
  378.         thumb.recycle();  
  379.       msg.thumbData = WechatShareUtil.bitmapToByteArray(thumbBitmap, true);  
  380.           
  381.         SendMessageToWX.Req req = new SendMessageToWX.Req();  
  382.         req.transaction = buildTransaction("video");  
  383.         req.message = msg;  
  384.         req.scene =  shareType;  
  385.         mWXApi.sendReq(req);  
  386.     }  
  387.        
  388.     private String buildTransaction(final String type) {  
  389.         return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();  
  390.     }  
  391. }  
MainActivity.java

[java] view plain copy
  1. package com.jackie.umeng.share;  
  2.   
  3. import com.jackie.umeng.share.WechatShareManager.ShareContentPicture;  
  4. import com.jackie.umeng.share.WechatShareManager.ShareContentText;  
  5. import com.jackie.umeng.share.WechatShareManager.ShareContentVideo;  
  6.   
  7. import android.app.Activity;  
  8. import android.content.Context;  
  9. import android.content.pm.PackageManager;  
  10. import android.os.Bundle;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. import android.widget.Toast;  
  15.   
  16. public class MainActivity extends Activity implements OnClickListener {  
  17.     private Button mShareText, mSharePicture, mShareVideo;  
  18.     private WechatShareManager mShareManager;   
  19.       
  20.     private Context mContext;  
  21.   
  22.     @Override  
  23.     protected void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.activity_main);  
  26.           
  27.         mShareText = (Button) findViewById(R.id.share_text);  
  28.         mSharePicture = (Button) findViewById(R.id.share_picture);  
  29.         mShareVideo = (Button) findViewById(R.id.share_video);  
  30.         mShareText.setOnClickListener(this);  
  31.         mSharePicture.setOnClickListener(this);  
  32.         mShareVideo.setOnClickListener(this);  
  33.           
  34.         mContext = this;  
  35.           
  36.         mShareManager = WechatShareManager.getInstance(mContext);  
  37.     }  
  38.   
  39.     @Override  
  40.     public void onClick(View v) {  
  41.         if (!isWebchatAvaliable()) {  
  42.             Toast.makeText(mContext, "请先安装微信", Toast.LENGTH_LONG).show();  
  43.             return;  
  44.         }  
  45.         switch (v.getId()) {  
  46.         case R.id.share_text:  
  47.             ShareContentText mShareContentText = (ShareContentText) mShareManager.getShareContentText("微信文本分享");  
  48.             mShareManager.shareByWebchat(mShareContentText, WechatShareManager.WECHAT_SHARE_TYPE_FRENDS);  
  49.             break;  
  50.         case R.id.share_picture:  
  51.             ShareContentPicture mShareContentPicture = (ShareContentPicture) mShareManager.getShareContentPicture(R.drawable.share);  
  52.             mShareManager.shareByWebchat(mShareContentPicture, WechatShareManager.WECHAT_SHARE_TYPE_FRENDS);  
  53.             break;  
  54.         case R.id.share_video:  
  55.             ShareContentVideo mShareContentVideo = (ShareContentVideo) mShareManager.getShareContentVideo("http://baidu.hz.letv.com/kan/agSlT?fr=v.baidu.com/");  
  56.             mShareManager.shareByWebchat(mShareContentVideo, WechatShareManager.WECHAT_SHARE_TYPE_FRENDS);  
  57.             break;  
  58.         default:  
  59.             break;  
  60.         }  
  61.     }  
  62.       
  63.     private boolean isWebchatAvaliable() {  
  64.         //检测手机上是否安装了微信  
  65.         try {  
  66.             getPackageManager().getPackageInfo("com.tencent.mm", PackageManager.GET_ACTIVITIES);  
  67.             return true;  
  68.         } catch (Exception e) {  
  69.             return false;  
  70.         }  
  71.     }  
  72. }  
0 0
原创粉丝点击