android JNI cocos2dx调用对话框

1、c++部分(Dialog.h、Dialog.cpp)

/* * Dialog.h * * Created on: 2015-5-13 * Author: lin */

#ifndef DIALOG_H_
#define DIALOG_H_

#include <jni.h>
#include "cocos2d.h"
#include "platform/android/jni/JniHelper.h"

#define Dialog_CLASS_NAME "com/DialogBox/org/JniTestHelper"

using namespace cocos2d;

void setPackageName(const char *packageName);

void exitApp();
extern "C"
{
void showTipDialog(const char *title, const char *msg);

void Java_com_DialogBox_org_JniTestHelper_setPackageName(JNIEnv *env, jobject thiz, jstring packageName);

void Java_com_DialogBox_org_JniTestHelper_exitApp(JNIEnv *env, jobject thiz);
}

#endif /* DIALOG_H_ */

/* * Dialog.cpp * * Created on: 2015-5-13 * Author: lin */
#include "Dialog.h"

void setPackageName(const char *packageName)
{
    CCLog("packageName: %s", packageName);
}

void exitApp()
{
    CCDirector::sharedDirector()->end();
}
extern "C"
{
void showTipDialog(const char *title, const char *msg)
    {
        JniMethodInfo t;
        if(JniHelper::getStaticMethodInfo(t, Dialog_CLASS_NAME, "showTipDialog", "(Ljava/lang/String;Ljava/lang/String;)V"))
        {
            jstring jTitle = t.env->NewStringUTF(title);
            jstring jMsg = t.env->NewStringUTF(msg);
            t.env->CallStaticVoidMethod(t.classID, t.methodID, jTitle, jMsg);
            t.env->DeleteLocalRef(jTitle);
            t.env->DeleteLocalRef(jMsg);
        }
    }

void Java_com_DialogBox_org_JniTestHelper_setPackageName(JNIEnv *env, jobject thiz, jstring packageName)
{
    const char *pkgName = env->GetStringUTFChars(packageName, NULL);
    setPackageName(pkgName);
    env->ReleaseStringUTFChars(packageName, pkgName);
}

    void Java_com_DialogBox_org_JniTestHelper_exitApp(JNIEnv *env, jobject thiz)
    {
        exitApp();
    }

}

2、java部分(JniTestHelper、DialogMessage、)

package com.DialogBox.org;


import android.os.Handler;
import android.os.Message;

public class JniTestHelper {
    private static Handler mHandler;
    public static void init(Handler handler)
    {
        JniTestHelper.mHandler = handler;
    }

    public static native void setPackageName(String packageName);

    public static native void exitApp();
    //c++函数调用java层函数,显示一个对话框
    private static void showTipDialog(final String title, final String text)  
    {
        Message msg = mHandler.obtainMessage();  
        msg.what = DialogBox.SHOW_DIALOG;  
        DialogMessage dm = new DialogMessage(title, text);  
        msg.obj = dm;  
        msg.sendToTarget();  
    }

}

package com.DialogBox.org;

public class DialogMessage {
    public String title;
    public String msg;

    public DialogMessage(String title, String msg) {
        super();
        this.title = title;
        this.msg = msg;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }

}
package com.DialogBox.org;

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

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class DialogBox extends Cocos2dxActivity{

    public static final int SHOW_DIALOG = 0x0001;
    public static SharedPreferences sharedPreferences;
    public static SharedPreferences.Editor editor;

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState); 
        JniTestHelper.init(mHandler);
        JniTestHelper.setPackageName(this.getPackageName());
        sharedPreferences = this.getSharedPreferences("cn",Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
        System.out.println(getInt("count", -1));
    }

    public Cocos2dxGLSurfaceView onCreateView() {
        Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
        // GuideLayer should create stencil buffer
        glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);

        return glSurfaceView;
    }    

    //显示对话框的函数
    private Handler mHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            switch(msg.what)
            {
            case SHOW_DIALOG:
                DialogMessage dm = (DialogMessage)msg.obj;
                new AlertDialog.Builder(DialogBox.this)
                .setTitle(dm.getTitle())
                .setMessage(dm.getMsg()).setNegativeButton("cancle", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("Ok", 
                        new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        JniTestHelper.exitApp();
                    }
                })
                .create().show();
                break;
            }
        }
    };

3、备注:Dialog_CLASS_NAME根据实际情况定义,
android.mk中的LOCAL_SRC_FILES添加Dialog.cpp 路径。

你可能感兴趣的:(android,jni)