首页 > 代码库 > Cocos2d-x调用Java 代码
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
Cocos2d-x调用Java 代码
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。