egret Android微端接入微信(二)分享网页类型

在之前文章微信登录基础上接入微信分享

这里面参考微信demo里面的网页类型分享

1、在MainActivity.java下添加代码

//微信网页类型分享
private void wxShare(String jsonString){
    if(api != null && api.isWXAppInstalled()) {
        Log.i(TAG,jsonString);
        String url = "";
        String title = "";
        String description = "";
        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            url = jsonObject.getString("link");
            title = jsonObject.getString("title");
            description = jsonObject.getString("desc");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = url;
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = title;
        msg.description = description;
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
        Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
        bmp.recycle();
        msg.thumbData = Util.bmpToByteArray(thumbBmp, true);

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("webpage");
        req.message = msg;
        req.scene = mTargetScene;
        api.sendReq(req);
    }else{
        Toast.makeText(this, "用户未安装微信", Toast.LENGTH_SHORT).show();
    }
}

2、在setExternalInsterfaces()方法中新增callNativeForShare消息注册

private void setExternalInterfaces() {
    launcher.setExternalInterface("callNativeForLogin", new INativePlayer.INativeInterface() {
        @Override
        public void callback(String s) {
            Log.d("Egret Launcher", s);
            getCode();
        }
    });
    launcher.setExternalInterface("callNativeForShare", new INativePlayer.INativeInterface() {
        @Override
        public void callback(String s) {
            Log.d("Egret Launcher", s);
            wxShare(s);
        }
    });
}

这里接收的参数是json字符串,获取js端传过来的链接,标题,描述

3、js端发送的代码参考如下

share(shareData) {
return new Promise(async (resolve) => {
console.log('weixin share:', shareData);
egret.ExternalInterface.call('callNativeForShare', JSON.stringify(shareData));
});
}

4、WXEntryActivity.java 方法onResp修改如下,区分回调的是登录还是分享

    
        // 第三方应用发送到微信的请求处理后的响应结果,会回调到该方法
    @Override
    public void onResp(BaseResp resp) {
        String result = "";
        Log.i(TAG,"onResp");
//        Toast.makeText(this, "baseresp.getType = " + resp.getType(), Toast.LENGTH_SHORT).show();

        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                if (resp.getType() == ConstantsAPI.COMMAND_SENDAUTH){
                    result = "用户同意";
                    if(resp instanceof SendAuth.Resp){
                        SendAuth.Resp newResp = (SendAuth.Resp)resp;
                        String code = newResp.code;
                        Log.i(TAG,"code:"+ code);
                        MainActivity.activity.callJStoCode(code);
                    }
                }else if(resp.getType() == ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX){
                    result = "分享成功";
                }
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                result = "用户取消";
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                result = "用户拒绝授权";
                break;
            case BaseResp.ErrCode.ERR_UNSUPPORT:
                result = "暂不支持";
                break;
            default:
                result = "未知错误";
                break;
        }

        Toast.makeText(this, result, Toast.LENGTH_LONG).show();
        finish();
    }


发表评论

(必填)

(必填)

(以便回访)

博客已稳定运行: 访问量:180,258 views 网站备案号:闽ICP备17026396号-1