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 路径。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值