* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/*#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) */
#include <jni.h>
#include "../../../../cocos2dx/platform/android/jni/JniHelper.h"
#include <android/log.h>
#include <string>
#include <stdio.h>
using namespace cocos2d;
using namespace std;
/*#endif */
extern "C" {
JNIEXPORT jint JNICALL Java_com_cocoa_HiWorld_stringZjy1(
JNIEnv * env, jobject obj);
};
JNIEXPORT jint JNICALL
Java_com_cocoa_HiWorld_stringZjy1(JNIEnv * env, jobject obj)
{
JniMethodInfo minfo;//JniHelper
/* 测试用方法 */
/*bool isHave = JniHelper::getStaticMethodInfo(minfo,"com/cocoa/HiWorld","loginGree", "()V"); //
if (isHave) {
//CCLog("有showText ");
minfo.env -> CallStaticVoidMethod(minfo.classID,minfo.methodID);
}else
{
//CCLog("没有方法showText");
}*/
/* 分享 */
/*//将c++中的string转换成java中的string
//char str[] = "test";
bool isHave = JniHelper::getStaticMethodInfo(minfo,"com/cocoa/HiWorld","shareSina", "(Ljava/lang/String;Ljava/lang/String;)V"); //
if (isHave) {
//CCLog("有share ");
jstring jstr = minfo.env->NewStringUTF("test1 share");
jstring jst = minfo.env->NewStringUTF("/data/data/com.cocoa/cy.png");
//jstring jst = minfo.env->NewStringUTF("");
minfo.env -> CallStaticVoidMethod(minfo.classID,minfo.methodID,jstr,jst);
}else
{
//CCLog("没有方法share");
}*/
/*
*java对应的签名
*boolean -----Z
*byte -----B
*char -----C
*short -----S
*int -----I
*long -----J
*float -----F
*double -----D
*void -----V
*Object -----Ljava/lang/String; L用/分割类的完整路径
*Array -----[Ljava/lang/String; [签名 [I
*/
/*
*call非静态java方法 对应的签名
*CallVoidMethod ---------void
*CallObjectMethod ---------jobject
*CallBooleanMethod ---------jboolean
*CallByteMethod ---------jbyte
*CallCharMethod ---------jchar
*CallShortMethod ---------jshort
*CallIntMethod ---------jint
*CallLongMethod ---------jlong
*CallFloatMethod ---------jfloat
*CallDoubleMethod ---------jdouble
*/
/*
*java类型对应本地类型
*boolean -----jboolean ---C/C++8位整型
*byte -----jbyte ---C/C++8位带符号整型
*char -----jchar ---
*short -----jshort ---
*int -----jint ---
*long -----jlong ---
*float -----jfloat ---
*double -----jdouble ---
*Object -----jobject ---
*Class -----jclass ---
*String -----jstring ---
*Object[] -----jobjectArray ---
*boolean[] -----jbooleanArray ---
*byte[] -----jbyteArray ---
*char[] -----jcharArray ---
*short[] -----jshortArray ---
*int[] -----jintArray ---
*long[] -----jlongArray ---
*float[] -----jfloatArray ---
*double[] -----jdoubleArray ---
*/
/* 设置高分 */
/*jint ind = 0;
jlong lsre = 2202l;
bool isHave = JniHelper::getStaticMethodInfo(minfo,"com/cocoa/HiWorld","setHighScore", "(IJ)V");
if (isHave) {
minfo.env -> CallStaticVoidMethod(minfo.classID,minfo.methodID,ind,lsre);
}*/
/* 成就解锁 */
/*jint aind = 0;
bool isHave = JniHelper::getStaticMethodInfo(minfo,"com/cocoa/HiWorld","unLock", "(I)V");
if (isHave) {
minfo.env -> CallStaticVoidMethod(minfo.classID,minfo.methodID,aind);
}*/
/* 测试用方法 */
bool isHave = JniHelper::getStaticMethodInfo(minfo,"com/cocoa/HiWorld","rtnActivity","()Ljava/lang/Object;");
jobject jobj;
if (isHave) {
jobj = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID);
}
//CCLog(" jobj存在");
/* 测试用方法,非静态无参数无返回值方法 */
/*isHave = JniHelper::getMethodInfo(minfo,"com/cocoa/HiWorld","showText", "()V");
if (isHave) {
minfo.env -> CallVoidMethod(jobj,minfo.methodID);
}*/
/* 测试用方法,非静态有java类型的String参数无返回值方法 */
/*isHave = JniHelper::getMethodInfo(minfo,"com/cocoa/HiWorld","showText", "(Ljava/lang/String;)V");
if (isHave) {
jstring jmsg = minfo.env->NewStringUTF("msg okey!");
minfo.env -> CallVoidMethod(jobj,minfo.methodID,jmsg);
}*/
/* 测试用方法,返回java类型的String,有java类型的String和int参数方法 */
/*isHave = JniHelper::getMethodInfo(minfo,"com/cocoa/HiWorld","showText", "(Ljava/lang/String;I)Ljava/lang/String;");
if (isHave) {
jstring jmsg = minfo.env->NewStringUTF("msg okey! return string");
jint index = 0;
minfo.env -> CallObjectMethod(jobj,minfo.methodID,jmsg,index);
}*/
/* 测试用方法,返回java类型的String[],有java类型的String[]和int参数方法 */
/*isHave = JniHelper::getMethodInfo(minfo,"com/cocoa/HiWorld","showText", "([Ljava/lang/String;I)[Ljava/lang/String;");
if (isHave) {
jobjectArray args = 0;
jstring str;
jsize len = 5;
const char* sa[] = {"Hi,","World!","JNI ","is ","fun"};
int i = 0;
args = minfo.env->NewObjectArray(len,minfo.env->FindClass("java/lang/String"),0);
for(i=0;i<len;i++){
str = minfo.env->NewStringUTF(sa[i]);
minfo.env->SetObjectArrayElement(args,i,str);
}
//minfo.env->GetStringArrayRegion(args,0,10,buf);
//jintArray jmsg = {1,2,3};
//minfo.env->NewStringUTF("msg okey! return string");
jint index = 0;
minfo.env -> CallObjectMethod(jobj,minfo.methodID,args,index);
}*/
/* 测试用方法,无返回类型,有java类型的int[]和int参数方法 */
/*isHave = JniHelper::getMethodInfo(minfo,"com/cocoa/HiWorld","testArr", "([II)V");
if (isHave) {
jint buf[]={7,5,8,9,3};
jintArray jintArr; //定义jint数组
jintArr = minfo.env->NewIntArray(5);
minfo.env->SetIntArrayRegion(jintArr,0,5,buf);
jint index = 0;
minfo.env -> CallVoidMethod(jobj,minfo.methodID,jintArr,index);
}*/
/* 测试用方法,无返回类型,有java类型的byte[]和int参数方法 */
isHave = JniHelper::getMethodInfo(minfo,"com/cocoa/HiWorld","testArr", "([BI)V");
if (isHave) {
jbyte buf[]={7,5,8,9,3};
jbyteArray jbyteArr; //定义jbyte数组
jbyteArr = minfo.env->NewByteArray(5);
minfo.env->SetByteArrayRegion(jbyteArr,0,5,buf);
jint index = 0;
minfo.env -> CallVoidMethod(jobj,minfo.methodID,jbyteArr,index);
}
return 1;
}
android
private static HiWorld hiWorld = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
hiWorld = this;
if (detectOpenGLES20()) {
// get the packageName,it's used to set the resource path
String packageName = getApplication().getPackageName();
super.setPackageName(packageName);
// set content
setContentView(R.layout.game_demo);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.window_title);
mGLView = (Cocos2dxGLSurfaceView) findViewById(R.id.game_gl_surfaceview);
mGLView.setTextField((Cocos2dxEditText) findViewById(R.id.textField));
mGLView.setEGLContextClientVersion(2);
mGLView.setCocos2dxRenderer(new Cocos2dxRenderer());
task = new TimerTask() {
@Override
public void run() {
// HiWorld.shoot(hiWorld);
Log.e("-------------------", "-------------------");
// 调用c++中的方法
System.out.println("------------------------"
+ stringZjy1());
}
};
timer = new Timer();
timer.schedule(task, 5000);
} else {
Log.d("activity", "don't support gles2.0");
finish();
}
...........
static {
System.loadLibrary("game");
}
// c++中調用的方法
public static Object rtnActivity() {
System.out.println("----------rtnActivity");
return hiWorld;
}
// c++中調用的方法,传String类型
public void showText(final String msg) {
// 添加到主线程
hiWorld.runOnUiThread(new Runnable() {
public void run() {
System.out.println("----------msg:"+msg);
}
});
}
//c++中調用的方法,传String类型和int类型
public String showText(final String msg,final int index) {
// 添加到主线程
hiWorld.runOnUiThread(new Runnable() {
public void run() {
System.out.println("----------msg:"+msg+"; index="+index);
}
});
return "okey String showText(final String msg,final int index)";
}
//c++中調用的方法,传String[]类型和int类型
public String[] showText(final String[] msg,final int index) {
String[] strArr = {"1","2","3","4","5"};
// 添加到主线程
hiWorld.runOnUiThread(new Runnable() {
public void run() {
for(String _str:msg){
System.out.println("----------String[] msg:"+_str+"; index="+index);
}
}
});
return strArr;
}
//c++中調用的方法,传int[]类型和int类型
public void testArr(final int msg[],final int index) {
// 添加到主线程
hiWorld.runOnUiThread(new Runnable() {
public void run() {
System.out.println("----------int[] msg len:"+msg.length);
for(int _bl:msg){
System.out.println("----------int[] msg:"+_bl+"; index="+index);
}
}
});
}
//c++中調用的方法,传int[]类型和int类型
public void testArr(final byte msg[],final int index) {
// 添加到主线程
hiWorld.runOnUiThread(new Runnable() {
public void run() {
System.out.println("----------byte[] msg len:"+msg.length);
for(int _bl:msg){
System.out.println("----------byte[] msg:"+_bl+"; index="+index);
}
}
});
}
// c++中的方法,java调用声明
public native int stringZjy1();
...........