Cocos2d-x calls android embedded browser to open web page

Source: Internet
Author: User

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 );

  1. }
2. Modify CCApplication. h and CCApplication. cpp under "cocos2dx \ platform \ android" and add functions

  1. Void CCApplication: openURL (const char * pszUrl)
  2. {
  3. JniMethodInfo minfo;
  4. If (JniHelper: getStaticMethodInfo (minfo,
  5. "Org/cocos2dx/application/ApplicationDemo ",
  6. "OpenURL ",
  7. "(Ljava/lang/String;) V "))
  8. {
  9. Jstring StringArg1 = minfo. env-> NewStringUTF (pszUrl );
  10. Minfo. env-> CallStaticVoidMethod (minfo. classID, minfo. methodID, StringArg1 );
  11. Minfo. env-> DeleteLocalRef (StringArg1 );
  12. Minfo. env-> DeleteLocalRef (minfo. classID );
  13. }
  14. }
3. Add the following statement to Cocos2dxActivity. java under src \ org \ cocos2dx \ lib:

  1. Private static Activity me = null;
  1. Protected void onCreate (final Bundle savedInstanceState ){
  2. ...
  3. Me = this;
  4. ...
  5. }
  6. // Return the Cocos2dxActivity object for calling the Function
  1. Public staticCocos2dxActivity getInstance (){
  2. Returnme;
  3. }
  4. Public void openURL (String url)
  5. {
  6. Intent I = new Intent (Intent. ACTION_VIEW );
  7. I. setData (Uri. parse (url ));
  8. Me. startActivity (I );
  9. }
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.



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.