今天研究Cocos2d-x,心血来潮的想做个2dx调用Android弹出框的效果.使用jni进行交互有木有.
C++调用Java , Java调用C++.(下载地址瞧下面)
-
void HelloWorld::menuCloseCallback(CCObject* pSender)
-
{
-
JniMethodInfo jmi;
-
if(JniHelper::getStaticMethodInfo(jmi , "com/jni/test/JniTest" , "sayHello" , "([Ljava/lang/String;)V"))
-
{
-
jclass str_cls = jmi.env->FindClass("java/lang/String");
-
-
jstring str1 = jmi.env->NewStringUTF("I'm a titile");
-
jstring str2 = jmi.env->NewStringUTF("Are yor exit game?");
-
-
jobjectArray arrs = jmi.env->NewObjectArray(2 , str_cls , 0);
-
jmi.env->SetObjectArrayElement(arrs , 0 , str1);
-
jmi.env->SetObjectArrayElement(arrs , 1 , str2);
-
jmi.env->CallStaticVoidMethod(jmi.classID , jmi.methodID , arrs);
-
}
-
}
-
-
extern "C"
-
{
-
/* 命名规则:Java_Java的包名_类名*/
-
void Java_com_jni_test_JniTest_sayHello()
-
{
-
CCLog("hello java , i'm c");
-
}
-
}
-
/****************************************************************************
-
Copyright (c) 2010-2012 cocos2d-x.org
-
-
http://www.cocos2d-x.org
-
-
Permission is hereby granted, free of charge, to any person obtaining a copy
-
of this software and associated documentation files (the "Software"), to deal
-
in the Software without restriction, including without limitation the rights
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-
copies of the Software, and to permit persons to whom the Software is
-
furnished to do so, subject to the following conditions:
-
-
The above copyright notice and this permission notice shall be included in
-
all copies or substantial portions of the Software.
-
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-
THE SOFTWARE.
-
****************************************************************************/
-
package com.jni.test;
-
-
import java.io.UnsupportedEncodingException;
-
-
import org.cocos2dx.lib.Cocos2dxActivity;
-
-
import android.app.AlertDialog;
-
import android.app.Dialog;
-
import android.content.DialogInterface;
-
import android.os.Bundle;
-
import android.os.Handler;
-
import android.os.Message;
-
-
public class JniTest extends Cocos2dxActivity{
-
-
private static AlertDialog mDialog = null;
-
-
private static Handler mHandler = new Handler(new Handler.Callback() {
-
@Override
-
public boolean handleMessage(Message msg) {
-
String[] str = (String[])msg.obj;
-
mDialog.setTitle(str[0]);
-
mDialog.setMessage(str[1]);
-
mDialog.show();
-
return true;
-
}
-
});
-
-
public static native void sayHello();
-
-
protected void onCreate(Bundle savedInstanceState){
-
super.onCreate(savedInstanceState);
-
-
mDialog = new AlertDialog.Builder(this).create();
-
mDialog.setButton("确定", new AlertDialog.OnClickListener() {
-
@Override public void onClick(DialogInterface dialog, int which) {
-
JniTest.this.finish();
-
}
-
});
-
mDialog.setButton2("取消", new AlertDialog.OnClickListener() {
-
@Override public void onClick(DialogInterface dialog, int which) {
-
-
}
-
});
-
mDialog.setButton3("HelloC", new AlertDialog.OnClickListener() {
-
@Override public void onClick(DialogInterface dialog, int which) {
-
// Java调用c代码
-
JniTest.this.sayHello();
-
}
-
});
-
}
-
-
static {
-
System.loadLibrary("game");
-
}
-
-
public static void sayHello(String[] str){
-
Message mes = new Message();
-
mes.obj = str;
-
mHandler.sendMessage(mes);
-
}
-
}
效果如下:C++调用Android对话框
Java调用C++打印字符串
代码地址(时间原因,代码未优化,参考下吧...):
更多Cocos2d-x开发博文尽在 Koyoter's Blog
今天研究Cocos2d-x,心血来潮的想做个2dx调用Android弹出框的效果.使用jni进行交互有木有.
C++调用Java , Java调用C++.(下载地址瞧下面)
- void HelloWorld::menuCloseCallback(CCObject* pSender)
- {
- JniMethodInfo jmi;
- if(JniHelper::getStaticMethodInfo(jmi , "com/jni/test/JniTest" , "sayHello" , "([Ljava/lang/String;)V"))
- {
- jclass str_cls = jmi.env->FindClass("java/lang/String");
- jstring str1 = jmi.env->NewStringUTF("I'm a titile");
- jstring str2 = jmi.env->NewStringUTF("Are yor exit game?");
- jobjectArray arrs = jmi.env->NewObjectArray(2 , str_cls , 0);
- jmi.env->SetObjectArrayElement(arrs , 0 , str1);
- jmi.env->SetObjectArrayElement(arrs , 1 , str2);
- jmi.env->CallStaticVoidMethod(jmi.classID , jmi.methodID , arrs);
- }
- }
- extern "C"
- {
- /* 命名规则:Java_Java的包名_类名*/
- void Java_com_jni_test_JniTest_sayHello()
- {
- CCLog("hello java , i'm c");
- }
- }
- /****************************************************************************
- Copyright (c) 2010-2012 cocos2d-x.org
- http://www.cocos2d-x.org
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- ****************************************************************************/
- package com.jni.test;
- import java.io.UnsupportedEncodingException;
- import org.cocos2dx.lib.Cocos2dxActivity;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- public class JniTest extends Cocos2dxActivity{
- private static AlertDialog mDialog = null;
- private static Handler mHandler = new Handler(new Handler.Callback() {
- @Override
- public boolean handleMessage(Message msg) {
- String[] str = (String[])msg.obj;
- mDialog.setTitle(str[0]);
- mDialog.setMessage(str[1]);
- mDialog.show();
- return true;
- }
- });
- public static native void sayHello();
- protected void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- mDialog = new AlertDialog.Builder(this).create();
- mDialog.setButton("确定", new AlertDialog.OnClickListener() {
- @Override public void onClick(DialogInterface dialog, int which) {
- JniTest.this.finish();
- }
- });
- mDialog.setButton2("取消", new AlertDialog.OnClickListener() {
- @Override public void onClick(DialogInterface dialog, int which) {
- }
- });
- mDialog.setButton3("HelloC", new AlertDialog.OnClickListener() {
- @Override public void onClick(DialogInterface dialog, int which) {
- // Java调用c代码
- JniTest.this.sayHello();
- }
- });
- }
- static {
- System.loadLibrary("game");
- }
- public static void sayHello(String[] str){
- Message mes = new Message();
- mes.obj = str;
- mHandler.sendMessage(mes);
- }
- }
效果如下:C++调用Android对话框
Java调用C++打印字符串
代码地址(时间原因,代码未优化,参考下吧...):
原文标题:【Cocos2d-x】cocos2d-x与Java层通过Jni进行交互