cocos3.x 接入微信无法调用回调函数onResp的问题

Posted 游戏编程技术学习_UE4 U3d Ogre Cocos 交流

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cocos3.x 接入微信无法调用回调函数onResp的问题相关的知识,希望对你有一定的参考价值。

要想顺利调用必须保证一下几点:

1.WXEntryActivity的包名必须正确,格式为你的APK包名+wxapi.WXEntryActivity(注意:是apk包名,而不是org.cocos2dx.cpp,我就是这个问题调了1天。。。。。)

2.WXEntryActivity在androidManifest.xml的配置

 <!--微信回调-->
        <activity
            android:name="xx.xxx.xxx.wxapi.WXEntryActivity"
            android:label="@string/app_name"
            android:exported="true">
            android:launchMode="singleTask"
        </activity>

3.在onCreate和onNewIntent

中调用   IWXAPI的handleIntent(this.getIntent(), this);

附上WXEntryActivity代码

package com.hongyou.hongyoudragonsky.wxapi;

/**
 * Created by Administrator on 2018/1/8.
 */

import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;
import android.util.Log;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;


import org.cocos2dx.cpp.WChatShare;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;
import org.cocos2dx.lib.Cocos2dxLuaJavaBridge;

/**
 * Created by Administrator on 2018/1/7.
 */
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

    public static String LogFlag = "WXEntryActivityTag";

    public static String ShareResult = "-1";
    private static int _shareCallBackLuaFunc = 0;            //分享回调lua接口


    //设置分享回调lua接口
    public static void SetShareCallBackLuaFunc(final int luaFunc) {
        //log
        Log.i(LogFlag, "----call WXEntryActivity.SetShareCallBackLuaFunc--------");

        _shareCallBackLuaFunc = luaFunc;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //log
        Log.i(LogFlag, "----call WXEntryActivity.onCreate--------");


        WChatShare.api.handleIntent(this.getIntent(), this);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        //log
        Log.i(LogFlag, "----call WXEntryActivity.onNewIntent--------");

        super.onNewIntent(intent);

        setIntent(intent);

        WChatShare.api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

        //log
        String logStr = "----call WXEntryActivity.onReq() ";
        Log.i(LogFlag, logStr);


        switch (baseReq.getType()) {
            case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
                Log.i(LogFlag, "type=COMMAND_GETMESSAGE_FROM_WX");
                break;
            case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
                Log.i(LogFlag, "type=COMMAND_SHOWMESSAGE_FROM_WX");
                break;
            default:
                break;
        }

        finish();
    }




    @Override
    public void onResp(BaseResp baseResp) {
        //log
        String logStr = "----call WXEntryActivity.onResp--0-- ";
        Log.i(LogFlag, logStr);


        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                ShareResult = "0";
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                ShareResult = "1";
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                ShareResult = " 2";
                break;
            case BaseResp.ErrCode.ERR_UNSUPPORT:
                ShareResult = "3";
                break;
            default:
                ShareResult = "4";
                break;
        }

        if (_shareCallBackLuaFunc > 0)
        {

            Cocos2dxGLSurfaceView.getInstance().queueEvent(new Runnable() {
                @Override
                public void run() {

                    Cocos2dxLuaJavaBridge.callLuaFunctionWithString(_shareCallBackLuaFunc, ShareResult);
                }
            });

        }

        finish();
    }



        @Override
        public void run() {
            Cocos2dxLuaJavaBridge.callLuaFunctionWithString(_shareCallBackLuaFunc, _result);
        }
    }


}

4.必须用你在微信开发平台审核通过的Appid和apk包签名

以上是关于cocos3.x 接入微信无法调用回调函数onResp的问题的主要内容,如果未能解决你的问题,请参考以下文章

TP5.1接入支付宝实现网页/APP支付完整请求回调流程(沙箱环境)

微信小程序Component组件调用回调函数this指向不是本页面

类中 dll 的回调函数

H5网站接入微信支付(H5支付+JSAPI支付)

H5网站接入微信支付(H5支付+JSAPI支付)

H5网站接入微信支付(H5支付+JSAPI支付)