码迷,mamicode.com
首页 > 微信 > 详细

微信分享封装类

时间:2016-09-06 19:55:01      阅读:453      评论:0      收藏:0      [点我收藏+]

标签:

 

 

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

import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.SendMessageToWX;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.mm.sdk.openapi.WXImageObject;
import com.tencent.mm.sdk.openapi.WXMediaMessage;
import com.tencent.mm.sdk.openapi.WXTextObject;
import com.tencent.mm.sdk.openapi.WXVideoObject;
import com.tencent.mm.sdk.openapi.WXWebpageObject;
import com.tencent.mm.sdk.platformtools.Util;
import com.xuehu365.xuehu.R;
import com.xuehu365.xuehu.model.Constant;
import com.xuehu365.xuehu.model.VideoEntity;
import com.xuehu365.xuehu.utils.LogHelp;

/**
 * Created by Administrator on 2016/9/6.
 */
public class WXShareUtils {

    private static IWXAPI wxApi;

    private static void init(Context context) {
        //实例化
        wxApi = WXAPIFactory.createWXAPI(context, Constant.wxAppId);
        wxApi.registerApp(Constant.wxAppId);
        //判断是否有安装微信
        if (!wxApi.isWXAppInstalled()) {
            Toast.makeText(context, "您还未安装微信客户端", Toast.LENGTH_SHORT).show();
            return;
        }
    }

    /*
    分享文本
     */
    public static void shareText(Context context, String text, String type) {
        init(context);
        LogHelp.i("wx","here");
        //初始化一个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();
        req.transaction = buildTransaction("text");
        req.message = msg;
        if (Constant.wxTalk.equals(type)) {
            req.scene = SendMessageToWX.Req.WXSceneSession;
        } else if (Constant.wxFriend.equals(type)) {
            req.scene = SendMessageToWX.Req.WXSceneTimeline;
        }
        //调用api接口发送数据到微信
        wxApi.sendReq(req);
    }


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

    /*
    图片分享
     */
    public static void shareImage(Context context,String imgUrl, String type){
        init(context);
        Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_logo);

        //初始化WXTextObject和WXMediaMessage对象
        WXImageObject imageObj = new WXImageObject(bmp);
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = imageObj;

        //设置缩略图
        Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp,400,400,true);
        bmp.recycle();
        msg.thumbData = Util.bmpToByteArray(thumbBmp,true);

        //构造一个Req
        SendMessageToWX.Req req= new SendMessageToWX.Req();
        req.transaction = buildTransaction("img");

        req.message = msg;
        if (Constant.wxTalk.equals(type)) {
            req.scene = SendMessageToWX.Req.WXSceneSession;
        } else if (Constant.wxFriend.equals(type)) {
            req.scene = SendMessageToWX.Req.WXSceneTimeline;
        }

        //调用api接口发送数据到微信
        wxApi.sendReq(req);
    }

    /*
    分享视频
     */
    public static void shareVideo(Context context, VideoEntity videoEntity, String type){
        init(context);
        //初始化一个WXVideoObject对象,填写url
        WXVideoObject video = new WXVideoObject();
        video.videoUrl= videoEntity.getVideoVid();

        //用一个WXVideoObject对象初始化一个WXMediaMessage对象,填写标题、描述
        WXMediaMessage msg = new WXMediaMessage(video);
        msg.title = videoEntity.getTitle();
        msg.description = videoEntity.getTitle();
        Bitmap thumb = BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_logo);
        msg.thumbData = Util.bmpToByteArray(thumb,true);

        //构造一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("video");
        req.message = msg;
        if (Constant.wxTalk.equals(type)) {
            req.scene = SendMessageToWX.Req.WXSceneSession;
        } else if (Constant.wxFriend.equals(type)) {
            req.scene = SendMessageToWX.Req.WXSceneTimeline;
        }

        //调用api接口发送数据到微信
        wxApi.sendReq(req);
    }


    /*
    分享网页
     */
    public static void shareWeb(Context context,String webUrl,String type){
        init(context);
        //初始化一个WXWebpageObject对象,填写URL
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = webUrl;

        //用WXWebpageObject对象初始化一个WXMediaMessage对象,填写标题、描述
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = webUrl;
        msg.description = webUrl;
        Bitmap thumb = BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_logo);
        msg.thumbData = Util.bmpToByteArray(thumb,true);

        //构造一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("webpage");
        req.message = msg;
        if (Constant.wxTalk.equals(type)) {
            req.scene = SendMessageToWX.Req.WXSceneSession;
        } else if (Constant.wxFriend.equals(type)) {
            req.scene = SendMessageToWX.Req.WXSceneTimeline;
        }

        //调用api接口发送数据到微信
        wxApi.sendReq(req);
    }

}

 

微信分享封装类

标签:

原文地址:http://www.cnblogs.com/anni-qianqian/p/5846918.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!