Implement interaction between c ++ and java through jni
1. java calls c ++
First, declare related methods in java
public static native void doLogin(String sid);public static native void doBindingSuc(String sid);
The call method is as follows:
Public void login () {this. runOnUiThread (new Runnable () {public void run () {// The following section provides the sdk parameters. You can skip TYGame. login ("F2", "L8", "6", bombEN4Android. this, new LoginResultListener () {@ Overridepublic void loginSuccess (final String sid) {// TODO login successful // myMessage. setText ("Login successful! \ N "+ sid); // Log. e ("sid", sid); bombEN4Android. doLogin (sid); // call the c ++ method} @ Overridepublic void loginFail () {// TODO Login Failed // myMessage. setText ("Login Failed! ") ;}}) ;}Public void doBinding () {this. runOnUiThread (new Runnable () {public void run () {TYGame. bindingAccount ("F", "L8", "6", bombEN4Android. this, new BindingAccountListener () {@ Overridepublic void success (String sid) {// myMessage. setText ("bound successfully! \ N "+ sid); bombEN4Android. doBindingSuc (sid); // call the c ++ method} @ Overridepublic void failure () {// TODO Auto-generated method stub // myMessage. setText ("binding failed! ");}});}});}
Implementation of related methods in c ++:
Void encode (JNIEnv * env, jobject thiz, jstring token) {const char * productStr = env-> token (token, NULL); string nowToken = productStr; LocalPlayerModel: sharedLocalPlayerModel () -> setUnionUid (ccs (nowToken); ccnotifcenter center: sharedNotificationCenter ()-> postNotification (ZQYJ_LOGIN); env-> ReleaseStringUTFChars (token, productStr );} void token (JNIEnv * env, jobject thiz, jstring token) {CCNotificationCenter: sharedNotificationCenter ()-> postNotification (token); const char * productStr = env-> GetStringUTFChars (token, NULL); string nowToken = productStr; LocalPlayerModel: sharedLocalPlayerModel ()-> setUnionUid (ccs (nowToken); LocalPlayerModel: sharedLocalPlayerModel ()-> doTyBounding (NULL ); env-> ReleaseStringUTFChars (token, productStr); // if no parameter is passed, the content of this method can be blank without releasing}
2. c ++ calls java
Call methods in c ++
Method definition:
void payTY(CCString *productId,CCString *orderId,int serverId,CCString *playerId, CCString *playerName, int price, CCString *productName);
Method implementation:
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); }}
Where,
static const string ANDROID_ACTIVITY_NAME="com/lmj/bombENAndroid/bombEN4Android";
The pay method in bombEN4Android. java is called.
Pay method in java:
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;}});}