Cococs2d-x C + + calls Java static functions

Source: Internet
Author: User

The Jnihelper class in cocos2d-x encapsulates what we need by acquiring the Java virtual machine through JNI, acquiring the current program's JNI environment, getting the Java class information that needs to be called through the JNI environment, and then getting the function information in the Java class that needs to be called. The corresponding Java functions are invoked using the class information and function information through the JNI environment.

Main interface:

static bool Getstaticmethodinfo (Jnimethodinfo &methodinfo, const char *classname, const char *methodname, const char * Paramcode);
function: Get Java class corresponding function information, CLASSID, Methodid, etc.
Arguments from left to right: the reference to the Jnimethodinfo, the path to the class, the name of the function calling Java, and the shorthand for invoking the Java function type: (parameter) return type
such as: (Ljava/lang/string;i) I means the function has string,int two parameters, the return value is int

Jint  Callstaticintmethod (Jclass clazz, Jmethodid Methodid,  
Function: Execute call via jnienv, return value int type
JNIEnv has a series of calling function formats: callstatic[return type]method, [return type] is different from function return type, corresponding to different function name
Arguments: When executing a call to a Java function, you need to pass in the arguments, add the parameters sequentially to ClassID, Methodid, and convert the C + + variable type to the Java type

such as: jstring Stringarg = Methodinfo.env->newstringutf (key);//Parameter conversion int Res=methodinfo.env->callstaticintmethod ( Methodinfo.classid, Methodinfo.methodid, stringarg,value);//execute parameter call and return value

Example:

Call Java static function Getint

static int getInt (const char* key,int value,const char* classPath) {Jnimethodinfo methodinfo;if (! Jnihelper::getstaticmethodinfo (MethodInfo, ClassPath, "GetInt", "(ljava/lang/string;i) I"))//Get Java class corresponding getInt function information { return-1;} Jstring Stringarg = Methodinfo.env->newstringutf (key);//Parameter conversion int Res=methodinfo.env->callstaticintmethod ( Methodinfo.classid, Methodinfo.methodid, stringarg,value);//execute the parameter call and return value Methodinfo.env->deletelocalref ( STRINGARG); Methodinfo.env->deletelocalref (METHODINFO.CLASSID); return res;}




Cococs2d-x C + + calls Java static functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.