cocos2d-x调用java中非静态方法以及静态方法注意事项以及示例

时间:2022-06-20 19:30:10


//如果需要调用一个类里边的非静态方法需要将类的实例取出来,然后用作classID

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //判断当前是否为Android平台

    JniMethodInfo minfo;//定义Jni函数信息结构体

    //getStaticMethodInfo 次函数返回一个bool值表示是否找到此函数

    bool isHave = JniHelper::getStaticMethodInfo(minfo,"com/ming/mygame/MyGame","testFun""()V");

    

    if (!isHave) {

        CCLog("jni:此函数不存在");

    }else{

        CCLog("jni:此函数存在");

        //调用此函数

        minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID);

    }

    CCLog("jni-java函数执行完毕");

    

    JniMethodInfo mObjInfo;//定义Jni函数信息结构体

    //getStaticMethodInfo 次函数返回一个bool值表示是否找到此函数

    bool isHaveObj = JniHelper::getStaticMethodInfo(mObjInfo,"com/ming/mygame/MyGame","rtnActivity""()Ljava/lang/Object;");

    jobject jobj;

    if (isHaveObj) {

        jobj = minfo.env->CallStaticObjectMethod(mObjInfo.classID, minfo.methodID);

    }

    

    JniMethodInfo methodInfo;

    bool isHaveMethod = JniHelper::getMethodInfo(methodInfo,"com/ming/mygame/MyGame","showMessage","()V");

    if (isHaveMethod) {

        methodInfo.env->CallVoidMethod(jobj,methodInfo.methodID);

    }

#endif



//一下即为测试代码中用到的java(Android代码)
//MyGame.java
package com.ming.mygame;

import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class MyGame extends Cocos2dxActivity{
    
     private static MyGame context;
     private static Handler mHandler;
    protected void onCreate(Bundle savedInstanceState){
          super.onCreate(savedInstanceState);    
          context = this;
        
          mHandler = new Handler(){
               @Override
               public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    switch (msg.what) {
                    case 0:
                         break;
                    case 1:
                         AlertDialog.Builder builder = new Builder(MyGame.this);
                         builder.setMessage("这是C++调用java代码弹出的对话框");
                         builder.setTitle("C++调用java");
                         builder.setPositiveButton("确认", new OnClickListener() {
                             
                              @Override
                              public void onClick(DialogInterface dialog, int which) {
                                  
                              }
                         });
                        
                         builder.setNegativeButton("取消", new OnClickListener() {
                             
                              @Override
                              public void onClick(DialogInterface dialog, int which) {
                                  
                              }
                         });
                         builder.create().show();
                         break;
                    }
               }
          };
     }

    public Cocos2dxGLSurfaceView onCreateView() {
         Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
         // MyGame should create stencil buffer
         glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);
        
         return glSurfaceView;
    }
   
    public static void testFun() {
        //这里是静态方法测试
        mHandler.sendEmptyMessage(0);
    }
   
    public void showMessage() {
runOnUiThread(new Runnable() { @Override public void run() { AlertDialog.Builder builder = new Builder(MyGame.this); builder.setMessage("这是C++调用java代码弹出的对话框"); builder.setTitle("C++调用java"); builder.setPositiveButton("确认"newOnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.setNegativeButton("取消"newOnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.create().show(); } });
     }
   
    public static Object rtnActivity() {
          return context;
     }

    static {
        System.loadLibrary("cocos2dcpp");
    }
}