标签:android style java os cti for
//如果需要调用一个类里边的非静态方法需要将类的实例取出来,然后用作classID
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //判断当前是否为Android平台 JniMethodInfo minfo;//定义Jni函数信息结构体 //getStaticMethodInfo 次函数返回一个bool值表示是否找到此函数 bool isHaveObj = JniHelper::getStaticMethodInfo(minfo,"com/ming/mygame/MyGame","rtnActivity", "()Ljava/lang/Object;”); //在这里要注意返回类型的写法 jobject jobj; if (isHaveObj) { jobj = minfo.env->CallStaticObjectMethod(minfo.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("确认", 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(); } });
}
public static Object rtnActivity() {
return context;
}
static {
System.loadLibrary("cocos2dcpp");
}
}
cocos2d-x调用java中非静态方法以及静态方法注意事项以及示例,布布扣,bubuko.com
cocos2d-x调用java中非静态方法以及静态方法注意事项以及示例
标签:android style java os cti for
原文地址:http://www.cnblogs.com/liumingming/p/3811830.html