JniUtil.h
#pragma once
#include
using namespace std;
namespace JniUtil
{
string callJava_getAppVersion();
bool callJava_copyText(string copyText);
string callJava_getTestAllString(bool b,int i,float f,double d,string s);
void callJava_callNativeFunShowText(bool b,int i,float f,double d,string s);
}
JniUtil.cpp
#include "JniUtil.h"
#include "cocos2d.h"
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
#include
#include "platform/android/jni/JniHelper.h"
#endif
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
#include "IosHelper.h"
#endif
#define JAVA_CLASSNAME "org/cocos2dx/cpp/AppActivity"
using namespace cocos2d;
namespace JniUtil
{
string callJava_getAppVersion()
{
string str = "";
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
JniMethodInfo minfo;
//在org.cocos2dx.cpp.AppActivity文件中查找static String getAppVersion();这静态方法是否存在。
bool isHave = JniHelper::getStaticMethodInfo(minfo,JAVA_CLASSNAME, "getAppVersion", "()Ljava/lang/String;");
if (isHave)
{
jstring jVersion = (jstring)minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID);
//使用jstring2string函数将返回的jstring类型的值转化为c++中的string类型
//string text = JniHelper::jstring2string(jVersion);
const char* version = minfo.env->GetStringUTFChars(jVersion,0);
str = version;
minfo.env->ReleaseStringUTFChars(jVersion, version);
minfo.env->DeleteLocalRef(minfo.classID);
cocos2d::log("JniFun call callJava_getAppVersion over!===%s",str.c_str());
}
else
{
cocos2d::log("JniFun call callJava_getAppVersion error!");
}
#endif
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
//IosHelper::HuiPay(kStr.c_str());
#endif
return str;
}
bool callJava_copyText(string copyText)
{
bool isSuccess = false;
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
JniMethodInfo minfo;
bool isHave = JniHelper::getStaticMethodInfo(minfo,JAVA_CLASSNAME, "copyText", "(Ljava/lang/String;)Z");
if (isHave)
{
jstring jCopyText = minfo.env->NewStringUTF(copyText.c_str());
jboolean jIsSuccess= minfo.env->CallStaticBooleanMethod(minfo.classID, minfo.methodID,jCopyText);
isSuccess = jIsSuccess;
minfo.env->DeleteLocalRef(minfo.classID);
cocos2d::log("JniFun call callJava_copyText over!");
}
else
{
cocos2d::log("JniFun call callJava_copyText error!");
}
#endif
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
//IosHelper::HuiPay(kStr.c_str());
#endif
return isSuccess;
}
string callJava_getTestAllString(bool b,int i,float f,double d,string s)
{
string str = "";
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
JniMethodInfo minfo;
bool isHave = JniHelper::getStaticMethodInfo(minfo,JAVA_CLASSNAME, "getTestAllString", "(ZIFDLjava/lang/String;)Ljava/lang/String;");
if (isHave)
{
jstring js = minfo.env->NewStringUTF(s.c_str());
jstring jRes = (jstring)minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID,b,i,f,d,js);
const char* res = minfo.env->GetStringUTFChars(jRes,0);
str = res;
minfo.env->ReleaseStringUTFChars(jRes, res);
minfo.env->DeleteLocalRef(js);
minfo.env->DeleteLocalRef(minfo.classID);
cocos2d::log("JniFun call callJava_getTestAllString over!==%s",str.c_str());
}
else
{
cocos2d::log("JniFun call callJava_getTestAllString error!");
}
#endif
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
//IosHelper::HuiPay(kStr.c_str());
#endif
return str;
}
void callJava_callNativeFunShowText(bool b,int i,float f,double d,string s)
{
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
JniMethodInfo minfo;
bool isHave = JniHelper::getStaticMethodInfo(minfo,JAVA_CLASSNAME, "callNativeFunShowText", "(ZIFDLjava/lang/String;)V");
if (isHave)
{
jstring js = minfo.env->NewStringUTF(s.c_str());
jstring jRes = (jstring)minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID,b,i,f,d,js);
const char* res = minfo.env->GetStringUTFChars(jRes,0);
string str = res;
minfo.env->ReleaseStringUTFChars(jRes, res);
minfo.env->DeleteLocalRef(js);
minfo.env->DeleteLocalRef(minfo.classID);
cocos2d::log("JniFun call callJava_callNativeFunShowText over!==%s",str.c_str());
}
else
{
cocos2d::log("JniFun call callJava_callNativeFunShowText error!");
}
#endif
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
//IosHelper::HuiPay(kStr.c_str());
#endif
}
}
package org.cocos2dx.cpp;
import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxHelper;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.util.AndroidException;
import android.view.WindowManager;
public class AppActivity extends Cocos2dxActivity
//public class AppActivity extends Cocos2dxHelper
{
//在java类中定义一个方法,用于提供给java调用C++
public static native void NativeFunShowText(String text);
private static AppActivity appActivity = null;
//剪切板管理工具类
private static ClipboardManager mClipboardManager;
//剪切板Data对象
private static ClipData mClipData;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
appActivity = this;
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mClipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
}
public static String getAppVersion() throws AndroidException
{
PackageInfo pInfo = getContext().getPackageManager().getPackageInfo(getContext().getPackageName(), 0);
String version = pInfo.versionName +" "+ pInfo.versionCode;
return version;
}
public static boolean copyText(String copyTxt)
{
//创建一个新的文本clip对象
mClipData = ClipData.newPlainText("Simple test", copyTxt);
//把clip对象放在剪贴板中
mClipboardManager.setPrimaryClip(mClipData);
return true;
}
public static String getTestAllString(boolean b,int i,float f,double d,String s)
{
final String str = "bool:"+ b + " int:" + i + " float:" + f + " double:" + d + " String:" + s;
System.out.println("----getTestAll----out runOnUiThread-----"+str);
//添加到主线程
appActivity.runOnUiThread(new Runnable(){
public void run(){
System.out.println("----getTestAll----in runOnUiThread-----"+str);
}
});
return str;
}
public static void callNativeFunShowText(boolean b,int i,float f,double d,String s)
{
final String str = "bool:"+ b + " int:" + i + " float:" + f + " double:" + d + " String:" + s;
System.out.println("----callNativeFunShowText----out runOnGLThread-----"+str);
//想从java代码来改变cocos2dxUI界面,需要在GL线程中运行,否则会崩溃
appActivity.runOnGLThread(new Runnable(){
public void run(){
System.out.println("----callNativeFunShowText----in runOnGLThread-----"+str);
NativeFunShowText(str);
}
});
}
}
#pragma once
namespace JniCallback
{
}
#include "JniCallback.h"
#include "cocos2d.h"
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
#include
#include "platform/android/jni/JniHelper.h"
#endif
using namespace cocos2d;
namespace JniCallback
{
extern "C"
{
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
//Java_:是格式,必须加的
//org_cocos2dx_cpp_AppActivity_NativeFunShowText:是包名+类名+方法名
JNIEXPORT void JNICALL Java_org_cocos2dx_cpp_AppActivity_NativeFunShowText (JNIEnv* env, jclass method, jstring param)
{
const char* data = env->GetStringUTFChars(param, 0);
cocos2d::log("Java_org_cocos2dx_cpp_AppActivity_NativeFunShowText---- :%s",data);
//do cocosUI something
env->ReleaseStringUTFChars(param, data);
}
#endif
}
}
LOCAL_SRC_FILES := hellocpp/main.cpp \
hellocpp/test.cpp \ <--为新添加的
可参考 [cocos2dx]Android.mk学习