首页 > 代码库 > 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 代码