微信分享封装类

 

 

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);
    }

}

 

微信分享封装类

上一篇:IOS微信中看文章跳转页面后点击返回无效


下一篇:实现手机扫描二维码页面登录,类似web微信-第四篇,服务器端