Cocos2d-x calls android embedded browser to open the web page, you can input the URL from the entrance, C ++ calls android api can be achieved. The method is also simple.
1. Modify CCApplication. h and CCApplication. cpp under "cocos2dx \ platform \ win32" and add functions
Header file Declaration
Void openURL (const char * pszUrl );
Cpp file implementation:
Void CCApplication: openURL (const char * pszUrl)
{
ShellExecuteA (NULL, "open", pszUrl, NULL, NULL, SW_SHOWNORMAL );
- }
2. Modify CCApplication. h and CCApplication. cpp under "cocos2dx \ platform \ android" and add functions
- Void CCApplication: openURL (const char * pszUrl)
- {
- JniMethodInfo minfo;
- If (JniHelper: getStaticMethodInfo (minfo,
- "Org/cocos2dx/application/ApplicationDemo ",
- "OpenURL ",
- "(Ljava/lang/String;) V "))
- {
- Jstring StringArg1 = minfo. env-> NewStringUTF (pszUrl );
- Minfo. env-> CallStaticVoidMethod (minfo. classID, minfo. methodID, StringArg1 );
- Minfo. env-> DeleteLocalRef (StringArg1 );
- Minfo. env-> DeleteLocalRef (minfo. classID );
- }
- }
3. Add the following statement to Cocos2dxActivity. java under src \ org \ cocos2dx \ lib:
- Private static Activity me = null;
- Protected void onCreate (final Bundle savedInstanceState ){
- ...
- Me = this;
- ...
- }
- // Return the Cocos2dxActivity object for calling the Function
- Public staticCocos2dxActivity getInstance (){
- Returnme;
- }
- Public void openURL (String url)
- {
- Intent I = new Intent (Intent. ACTION_VIEW );
- I. setData (Uri. parse (url ));
- Me. startActivity (I );
- }
Now you can call it in the cocos2d-x
First, C ++ to call java code, the cocos2d-x to call android api must use the jni Library
# If (CC_TARGET_PLATFORM = CC_PLATFORM_ANDROID)
# Include <jni. h>
# Include "platform/android/jni/JniHelper. h"
# Endif
Now, where you need to call:
# If (CC_TARGET_PLATFORM = CC_PLATFORM_ANDROID) JniMethodInfo minfo; // getStaticMethodInfo: determines whether a Java static function exists and saves the information to minfo. // parameter 1: JniMethodInfo // parameter 2: path of this class + class name // parameter 3: Java function name // parameter 4: function parameter type and return value type // return a bool, whether to find the function bool isHave = JniHelper: getStaticMethodInfo (minfo, "com/yipingtai/org/Webopen", "getInstance", "() Lcom/yipingtai/org/Webopen; "); jobject jobj; // save object if (isHave) {// call static getInstance here to return the web class object. Jobj = minfo. env-> CallStaticObjectMethod (minfo. classID, minfo. methodID); isHave = JniHelper: getMethodInfo (minfo, "com/yipingtai/org/Webopen", "openWebview", "(Ljava/lang/String;) V "); if (isHave) {jstring url = minfo. env-> NewStringUTF ("http://www.baidu.com"); // call openWebview, parameter 1: Test object parameter 2: Method ID parameter 3: parameter (No parameter is written)
minfo.env->CallVoidMethod(jobj, minfo.methodID, url);
}}#else CCDirector::sharedDirector()->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0);#endif#endif
The jni type signature is used here for a brief introduction.
Signature-type: Z: bool B: byte C: char S: short I: int J: long F: float D: double L: fully qualified class name [: Array
The class signature rule is composed of three parts: "L + fully qualified class name +;". The fully qualified class names are separated by "/" instead of ". "or" _ "are separated.
For example, java method: long fun (int n, String str, int [] arr );
The signature is: (ILjava/lang/String; [I) the content in the J brackets is divided into three parts, with no space between them, that is, "I" and "Ljava/lang/String; "and" [I ", representing int, String, and int [] respectively. The signature of the return value type is displayed outside the brackets, and J indicates the long type.
For more information, refer to Baidu's JNI method signature rules.