1.头文件
- #include <jni.h>
- #include "platform/android/jni/JniHelper.h"
- #include <android/log.h>
2. jstring 与 char * 的转换
- char* jstringTostring(JNIEnv* env, jstring jstr)
- {
- char* rtn = NULL;
- jclass clsstring = env->FindClass("java/lang/String");
- jstring strencode = env->NewStringUTF("utf-8");
- jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B");
- jbyteArray barr= (jbyteArray)env->CallObjectMethod(jstr, mid, strencode);
- jsize alen = env->GetArrayLength(barr);
- jbyte* ba = env->GetByteArrayElements(barr, JNI_FALSE);
- if (alen > 0)
- {
- rtn = (char*)malloc(alen + 1);
- memcpy(rtn, ba, alen);
- rtn[alen] = 0;
- }
- env->ReleaseByteArrayElements(barr, ba, 0);
- return rtn;
- }
3.1 Java - 调用- C(C代码)
- jint Java_com_elextech_ccgoe_GooglePay_nativePayOK(JNIEnv *env, jobject thiz, jint a, jstring b)
- {
- CCLOG("********** nativePayOK*******");
- Pay_ID = a;
- if(Pay_ID==0){
- CCLOG("Error in pay");
- return -1;
- }
- char * re ;
- re = jstringTostring(env , b);
- sprintf(Pay_Check,"%s",re);
- CCLOG("*****Pay_id****%d",Pay_ID);
- CCLOG("Pay_Check :%s",Pay_Check);
- GameBoard::sharedGameBoard()->paymentAddPoints(Pay_ID,"",Pay_Check);
- return 1;
- }
3.2 Java - 调用- C(java代码)
- private native int nativePayOK(int a , String b);
- public void onSuccessPay_byProductId(Order order) {
- int productId = Integer.parseInt(order.getProductId());
- Log.i("jnitest", "------nativePayOK---order.getProductId():"+productId);
- nativePayOK(productId, order.getOrderId());
- }
4.1 C - 调用- Java (java代码)
- public static void Click_pay(int pay_id){
- Log.i("jnitest", "------------goolge pay---pay_id:"+pay_id);
- payItem.setItemType(PayItemData.ITEM_TYPE_INAPP);
- payItem.setItemId(""+pay_id);
- GamePayImpl.getInstance().onSubmitPayAction(payItem);
- }
4.2 C - 调用- Java (C代码)
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
- JniMethodInfo minfo;
- bool isHave;
- if (gameBoardViewDelegate->if_QQLogin())
- {
- CCLOG("qq pay");
- isHave = JniHelper::getStaticMethodInfo(minfo, "com/elextech/goeqq/QQActivity","Click_qq_pay","(I)V");
- }
- else
- {
- CCLOG("google pay ");
- isHave = JniHelper::getStaticMethodInfo(minfo, "com/elextech/ccgoe/GooglePay","Click_pay","(I)V");
- }
- jobject jobj;
- if (!isHave) {
- CCLOG("jni: no method");
- }
- jobj = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID,pay_id);
- CCLOG("jni-java over");
- #endif
4.3 C传jstring给java
- JniMethodInfo minfo;
- if (! JniHelper::getStaticMethodInfo(minfo, "com/elextech/goelib/GOE", "umeng_event", "(Ljava/lang/String;)V") ) {
- CCLOG("jni: no method");
- return;
- }
- jstring jevent=minfo.env->NewStringUTF(event);
- minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID, jevent);
- minfo.env->DeleteLocalRef(jevent);
5. Java 传byte[]在C中的转换
- jint Java_com_elextech_goeqq_QQActivity_netiveQQLoginOK(JNIEnv *env, jobject thiz, jstring a, jbyteArray b)
- jbyte * olddata = (jbyte*)env->GetByteArrayElements(b, 0);
- jsize oldsize = env->GetArrayLength(b);
- unsigned char* bytearr = (unsigned char*)olddata;
- int len = (int)oldsize;