1、java调用c++
首先,在java中声明相关方法
public static native void doLogin(String sid); public static native void doBindingSuc(String sid);
public void login() { this.runOnUiThread(new Runnable() { public void run() { //下面部分为sdk提供的参数,可以不用管 TYGame.login("F2", "L8", "6", bombEN4Android.this, new LoginResultListener() { @Override public void loginSuccess(final String sid) { // TODO 登陆成功 // myMessage.setText("登陆成功!\n" + sid); // Log.e("sid", sid); bombEN4Android.doLogin(sid); //调用c++方法 } @Override public void loginFail() { // TODO 登陆失败 // myMessage.setText("登陆失败!"); } }); } }); } public void doBinding() { this.runOnUiThread(new Runnable() { public void run() { TYGame.bindingAccount("F", "L8", "6", bombEN4Android.this, new BindingAccountListener() { @Override public void success(String sid) { // myMessage.setText("绑定成功!\n" + sid); bombEN4Android.doBindingSuc(sid); //调用c++方法 } @Override public void failure() { // TODO Auto-generated method stub // myMessage.setText("绑定失败!"); } }); } }); }
c++中相关方法的实现:
void Java_com_lmj_bombENAndroid_bombEN4Android_doLogin(JNIEnv *env, jobject thiz, jstring token) { const char *productStr = env->GetStringUTFChars(token, NULL); string nowToken = productStr; LocalPlayerModel::sharedLocalPlayerModel()->setUnionUid(ccs(nowToken)); CCNotificationCenter::sharedNotificationCenter()->postNotification(ZQYJ_LOGIN); env->ReleaseStringUTFChars(token, productStr); } void Java_com_lmj_bombENAndroid_bombEN4Android_doBindingSuc(JNIEnv *env, jobject thiz, jstring token) { CCNotificationCenter::sharedNotificationCenter()->postNotification(ZQYJ_LOGIN); const char *productStr = env->GetStringUTFChars(token, NULL); string nowToken = productStr; LocalPlayerModel::sharedLocalPlayerModel()->setUnionUid(ccs(nowToken)); LocalPlayerModel::sharedLocalPlayerModel()->doTyBounding(NULL); env->ReleaseStringUTFChars(token, productStr); //如果没传参数,那么这个方法的内容可以为空,不用释放 }
c++中调用方法
方法的定义:
void payTY(CCString *productId,CCString *orderId,int serverId,CCString *playerId, CCString *playerName, int price, CCString *productName);
void AndroidUtils::payTY(CCString *productId,CCString *orderId,int serverId,CCString *playerId, CCString *playerName, int price, CCString *productName) { JniMethodInfo minfo; string s="()L"+ANDROID_ACTIVITY_NAME+";"; bool isHave = JniHelper::getStaticMethodInfo(minfo,ANDROID_ACTIVITY_NAME.c_str(),"getInstance",s.c_str()); jobject jobj; if (isHave) { jobj = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID); } isHave = JniHelper::getMethodInfo(minfo,ANDROID_ACTIVITY_NAME.c_str(),"pay","(Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;ILjava/lang/String;)V"); if (isHave) { jstring str1 = minfo.env->NewStringUTF(productId->getCString()); jstring str2 = minfo.env->NewStringUTF(orderId->getCString()); jstring str3 = minfo.env->NewStringUTF(playerId->getCString()); jstring str4 = minfo.env->NewStringUTF(playerName->getCString()); jstring str5 = minfo.env->NewStringUTF(productName->getCString()); minfo.env->CallVoidMethod(jobj, minfo.methodID, str1,str2,serverId,str3,str4,price, str5); minfo.env->DeleteLocalRef(str1); minfo.env->DeleteLocalRef(str2); minfo.env->DeleteLocalRef(str3); minfo.env->DeleteLocalRef(str4); minfo.env->DeleteLocalRef(str5); } }其中,
static const string ANDROID_ACTIVITY_NAME="com/lmj/bombENAndroid/bombEN4Android";
java中的pay方法:
public void pay(final String productId, final String orderId, final int serverId, final String playerId, final String playerName, int price, final String productName) { this.runOnUiThread(new Runnable() { public void run() { System.out.println("123"); System.out.println(productName); System.out.println(orderId); System.out.println(playerId); System.out.println(playerName); System.out.println("456"); String _playerName = null; String _productName = null; } }); }
原文地址:http://blog.csdn.net/somestill/article/details/44345873