Android 分享微信好友 朋友圈

分享到:

第三方应用,可以调用微信分享,把链接,文字,各种media,分享到微信好友或者微信朋友圈,步骤:

 

package com.edaixi.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.Toast;
import com.edaixi.activity.R;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXWebpageObject;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class WX_Share implements IWXAPIEventHandler{
    private IWXAPI api;
    private Context context;
    private int flag;//0 表示分享给微信好友哦,1表示分享到朋友圈

    public WX_Share(Context context,int flag) {
        super();
        this.context = context;
        this.flag = flag;
        registWX();
    }

    public void registWX() {
        api = WXAPIFactory.createWXAPI(context, Constants.APP_ID, true);
        api.registerApp(Constants.APP_ID);
    }

    public void share2WX() {  
        if (!api.isWXAppInstalled()) {  
            Toast.makeText(context, "您还未安装微信客户端", Toast.LENGTH_SHORT).show();  
            return;  
        }  
        WXWebpageObject webpage = new WXWebpageObject();  
        webpage.webpageUrl = "http://www.edaixi.com";  
        WXMediaMessage msg = new WXMediaMessage(webpage);  
      
        msg.title = "my test title...";  
        msg.description = context.getResources().getString(R.string.wx_share_content);  
        Bitmap thumb = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);  
        msg.setThumbImage(thumb);  
        
        SendMessageToWX.Req req = new SendMessageToWX.Req();  
        req.transaction = String.valueOf(System.currentTimeMillis());  
        req.message = msg;  
        req.scene = flag;  
        api.sendReq(req);  
    }

    @Override
    public void onReq(BaseReq arg0) {
        
    }

    @Override
    public void onResp(BaseResp resp) {
        Toast.makeText(context, "openid = " + resp.openId, Toast.LENGTH_SHORT).show();
        
        if (resp.getType() == ConstantsAPI.COMMAND_SENDAUTH) {
            Toast.makeText(context, "code = " + ((SendAuth.Resp) resp).code, Toast.LENGTH_SHORT).show();
        }
        
        String result = "";
        
        switch (resp.errCode) {
        case BaseResp.ErrCode.ERR_OK:
            result = "-sucessful-";
            break;
        case BaseResp.ErrCode.ERR_USER_CANCEL:
            result = "-cancle-";
            break;
        case BaseResp.ErrCode.ERR_AUTH_DENIED:
            result = "-error code-";
            break;
        default:
            result = "-unknow  error-";
            break;
        }
        
        Toast.makeText(context, result, Toast.LENGTH_LONG).show();
    
    }  
}
昵    称:
验证码:

相关文档: