微信分享登录

一、导入依赖

api 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'

二、注册

mWxApi = WXAPIFactory.createWXAPI(this, WE_CHAT_APP_ID, false);
mWxApi.registerApp(WE_CHAT_APP_ID);

三、创建回调Activity

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    /**
     * 登录
     */
    private static final int RETURN_MSG_TYPE_LOGIN = 1;

    /**
     * 分享
     */
    private static final int RETURN_MSG_TYPE_SHARE = 2;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        App.INSTANCE.mWxApi.handleIntent(getIntent(), this);
    }

    @Override
    public void onResp(BaseResp resp) {
        switch (resp.getType()) {
            case RETURN_MSG_TYPE_LOGIN:
                weChatLogin(resp);
                break;
            case RETURN_MSG_TYPE_SHARE:
                weChatShare(resp);
                break;
        }

        finish();
    }

    private void weChatShare(BaseResp resp) {
        switch (resp.errCode){
            case BaseResp.ErrCode.ERR_OK:
                Androids.makeText("分享成功");
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                Androids.makeText("分享取消");
                break;

            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                Androids.makeText("分享失败");
                break;
        }
    }    
}

四、微信分享

WXWebpageObject webPage = new WXWebpageObject();

webPage.webpageUrl = "https://www.baidu.com/";
WXMediaMessage msg = new WXMediaMessage(webPage);
msg.title = "百度";
msg.description = "BAT";
msg.thumbData = bitmap.toByteArray();

SendMessageToWX.Req req = new SendMessageToWX.Req();

req.scene = SendMessageToWX.Req.WXSceneSession;
// req.scene = SendMessageToWX.Req.WXSceneTimeline; 朋友圈
req.message = msg;
req.transaction = String.valueOf(System.currentTimeMillis());
App.INSTANCE.mWxApi.sendReq(req);
五、授权登录
SendAuth.Req req = new SendAuth.Req();
req.scope = "snsapi_userinfo";
req.state = "eth_mining_login"; // 自定义
App.INSTANCE.mWxApi.sendReq(req);

六、注意事项

  • 官网注册时填写的应用签名为无符号小写md5

你可能感兴趣的:(微信分享登录)