Cocos2d-x调用Java 代码
Java代码:
package com.dishu; import com.dishu.org.R; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Looper; public class Dialog { public static Dialog ins = null; public static Dialog getInstance(){ if (ins == null){ ins = new Dialog(); } return ins; } private Activity _activity; public void initRoot(Activity root) { _activity = root; } public void showQuitDialog() { _activity.runOnUiThread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub AlertDialog.Builder builder = new AlertDialog.Builder(_activity); builder.setMessage(R.string.msg); builder.setPositiveButton(R.string.sure, new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialoginterface, int i){ //按钮事件 _activity.finish(); System.exit(0); } }); builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialoginterface, int i){ //按钮事件 } }); builder.create().show(); } }); } }
public class AppActivity extends Cocos2dxActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Dialog.getInstance().initRoot(this); } }
cocos2dx代码:
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) #include <jni.h> #include "platform/android/jni/JniHelper.h" #include <android/log.h> #endif
// JNI call test #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) JniMethodInfo minfo; jobject jobj; if (JniHelper::getStaticMethodInfo(minfo, "com/dishu/Dialog", "getInstance", "()Lcom/dishu/Dialog;")) { jobj = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID); if (JniHelper::getMethodInfo(minfo, "com/dishu/Dialog", "showQuitDialog", "()V")) { minfo.env->CallVoidMethod(jobj, minfo.methodID); } } #endif
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。