要想顺利调用必须保证一下几点:
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包签名