1.cpp掉java非static方法 无参数 无返回值
(1).头文件引入
(2).按钮事件代码
调用非静态方法的关键代码
CallStaticVoidMethod
getMethodInfo
(3).java非static方法
2. cpp调用java static方法
cpp
getStaticMethodInfo
CallStaticVoidMethod
CallStaticIntMethod
CallStaticObjectMethod
string(string)
- void HelloWorld::menuCloseCallback(CCObject* pSender) {
- bool isHave;
- const char* msgRet;
- JniMethodInfo minfo;
- const char* fileName = "testWei";
- isHave = JniHelper::getStaticMethodInfo(minfo, "org/test/MoneyStatic",
- "sendWeibo", "(Ljava/lang/String;)Ljava/lang/String;"); //string参数返回string
- if (isHave) {
- jstring jTitle = minfo.env->NewStringUTF(fileName);
- jobject obj = minfo.env->CallStaticObjectMethod(minfo.classID,
- minfo.methodID, jTitle);
- jstring jRet = (jstring) obj;
- msgRet = (minfo.env)->GetStringUTFChars(jRet, NULL);
- }
- }
int(int)
bool isHave = JniHelper::getStaticMethodInfo(minfo, "org/test/MoneyStatic", "buyNumber", "(I)I");
jint _int = minfo.env->CallStaticIntMethod(minfo.classID, minfo.methodID, 333);
void(void)
bool isHave = JniHelper::getStaticMethodInfo(minfo, "org/test/MoneyStatic", "judgeUsers", "()V");
minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID);
java
- //string(string)
- public static String sendWeibo(String pCon) {
- System.out.println("sendWeibo : " + pCon);
- Message msg = mHandler.obtainMessage();
- msg.what = Demo.SHOW_DIALOG;
- msg.sendToTarget();
- return "";
- }
//int(int)
public static int buyNumber(int _int) {
System.out.println("buyNumber : " + _int);return _int+1000;
}
3.java调用cpp
原理:通过native调用cpp的可以返回参数
java
public static native void cardJudge(int pJudge); //其他方法调用此方法 类名.方法名
public static native void displayProgress(String numString); //
cpp
//pJudge上面传过来的参数
//org_test包名、MoneyStatic类名、cardJudge方法名
返回参数int
extern "C" {
void Java_org_test_MoneyStatic_cardJudge(JNIEnv *env, jobject thiz, jint pJudge){
if (pJudge == 1) {
} else { }
}
}
返回参数string
extern "C" {
void Java_org_mm_MyStatic_displayProgress(JNIEnv *env, jobject thiz, jstring text) {
const char* pszText = env->GetStringUTFChars(text, NULL);
}
}
4.转换
http://blog.csdn.net/lizhiguo0532/article/details/7219357
(1) jstring-->const char*
const char* pszText = env->GetStringUTFChars(text, NULL);
(2)const char*--> jstring
jstring str = env->newStringUTF("HelloJNI");
(3)jint --- int