简体   繁体   中英

Cocos2d-x Launching a URL on Android

我想从我的游戏中打开由cocos2dx 2.2.2创建的URL,所以任何帮助

If you want to open browser app, you should use Intent system in Java. To call Java code from C++, you should use JNI .

You can create method, that open URLs inside cocos2d-x's Java code. It's most simpliest way.

  1. Find org.cocos2dx.lib.Cocos2dxGLSurfaceView java class (location: cocos2d/platform/android/java )
  2. Add method to this class

     public static void openWebURL( String inURL ) { if (mCocos2dxGLSurfaceView != null) { Intent browse = new Intent( Intent.ACTION_VIEW , Uri.parse( inURL ) ); browse.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); mCocos2dxGLSurfaceView.getContext().startActivity( browse ); } } 
  3. Include JniHelper in C++ source file

     #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) #include "JniHelper.h" #endif 
  4. Add code, that calls Java method into right place

     #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) JniMethodInfo t; if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxGLSurfaceView", "openWebURL", "(Ljava/lang/String;)V")) { jstring stringArg = t.env->NewStringUTF(yourWebUrl); t.env->CallStaticIntMethod(t.classID, t.methodID, stringArg); t.env->DeleteLocalRef(stringArg1); t.env->DeleteLocalRef(t.classID); } #endif 

More correct way is to define Java method in your basic Activity class (that extends Cocos2dxActivity ). You should handle application lifecycle to store correct static reference to your activity in Java code, or store local reference to activity object in C++ code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM