Cocos2d-x C ++ call Android pop-up prompt box

Source: Internet
Author: User

The Cocos2d-x provides a JniHelper class for us to operate on Jni. (PS: Get a day to write code to operate Jni, but always error, poor technology had to use Cocos2d-x ready-made ). We have an Android project: CocosDemoAndroid and A Win32 project: Cocos2dDemo. win32, which is essential... There is a Scene in the C ++ Win32 project. click the button to call the Jni function. [Cpp] void CallJni: getInputCallBack (CCObject * pSender) {g_AndroidPlatform.showDialog ();} This g_AndroidPlatform.showDialog (); Use the JniHelper class in this function to use Jni. [Cpp] void AndroidPlatform: showDialog () {CCLog ("showDialog"); # ifdef ANDROID // The following uses the jnihelper class to call Android Static JniMethodInfo methodInfo; // The jni function information struct/* getStaticMethodInfo obtains the MethodId and ClassId. The ClassID is obtained by the class name in the Activity, and the MethodId is obtained by the function name */bool isHave = JniHelper :: getStaticMethodInfo (methodInfo, "com/example/cocosdemoandroid/MainActivity", "showMessage", "() V"); if (isHave) {/* call this function */methodInfo. env-> C AllStaticVoidMethod (methodInfo. classID, methodInfo. methodID) ;}# endif} the key code in the C ++ layer is the two. When using JniHelper, do not forget to add the header file [cpp] # ifdef ANDROID # include <jni. h> # include "platform/android/jni/JniHelper. h "# endif let's look at the Android-layer code. From the above C ++ code, we can see that the project package name is com. example. cocosdemoandroid Activity: MainActivity [java] public class MainActivity extends Cocos2dxActivity {public static Context mContext;/***** Message Type *****/private static final int SHOW_MESSAGE = 0; private static Handler handler = new Handler () {@ Override public void handleMessage (Message msg) {// accept the Message and then do the relevant thing if (msg. what = SHOW_MESSAGE) {Log. d ("CocosDemoAndroid", Strin G. valueOf (msg. obj); // obtain the parameter data passed by Handler AlertDialog. builder builder = new Builder (mContext); builder. setTitle ("C ++ calls Android"); builder. setMessage ("C ++ call the Android display pop-up box"); builder. show ();} super. handleMessage (msg) ;}}; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); mContext = MainActivity. this ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true;} public static void showMessage () {Log. d ("showMessage", "showMessage"); Message msg = new Message (); msg. what = SHOW_MESSAGE; msg. obj = "showmessage demos"; // The handler parameter can be passed. sendMessage (msg);} public native int InitJni (); static {System. loadLibrary ("CocosDemo ");}} For Android: Why do I use Handle sendMessage ()?? If a prompt box is displayed directly in showMessage, the system will crash and prompt Can't create handle inside thread that has not called logoff. prepare () is attached to Android. mk file: [cpp] LOCAL_PATH: = $ (call my-dir) RAKNET_LIB_PATH: = .. /.. /.. /.. /RakNet_PC-4.08/Android/obj/local/$ (TARGET_ARCH_ABI)/libRakNet. a $ (call import-add-path, $ (Cocos2d) \ $ (call import-add-path, $ (Cocos2d) \ cocos2dx \ platform \ third_party \ android \ prebuilt) \ include $ (CLEAR_VARS) LOCAL_MODULE: = libRakNet LOCAL_SRC_FILES: = $ (bytes) include $ (PREBUILT_STATIC_LIBRARY) include $ (CLEAR_VARS) LOCAL_MODULE: = CocosDemo_shared LOCAL_CFLAGS: =-w-DANDROID-DRAPIDXML_NO_EXCEPTIONS LOCAL_MODULE_FILENAME: = libCocosDemo LOCAL_SRC_FILES: = jni. cpp \.. /.. /Classes/TristanaLayer. cpp \.. /.. /Classes/AppDelegate. cpp \.. /.. /Classes/HelloWorldScene. cpp \.. /.. /Classes/CControlTest. cpp \.. /.. /Classes/CocosDemoMenu. cpp \.. /.. /Classes/CCScrollViewTest. cpp \.. /.. /Classes/CocosRakNet. cpp \.. /.. /Classes/MissCardSprite. cpp \.. /.. /Classes/MoveWithMouse. cpp \.. /.. /Classes/TransCards. cpp \.. /.. /Classes/AndroidPlatform. cpp \.. /.. /.. /public/network/MissGameNetWork. cpp \.. /.. /Classes/CallJni. cpp LOCAL_C_INCLUDES: = $ (LOCAL_PATH )/.. /.. /Classes \ $ (LOCAL_PATH )/.. /.. /.. /public \ $ (LOCAL_PATH )/.. /.. /.. /public/network \ $ (RakNet)/Source \ $ (Cocos2d)/cocos2dx \ $ (Cocos2d)/cocos2dx/include \ $ (Cocos2d) /cocos2dx/kazmath/include \ $ (Cocos2d)/cocos2dx/platform/android \ $ (Cocos2d)/cocos2dx/platform/third_party/android \ $ (Cocos2d) /external \ $ (Cocos2d)/CocosDenshion/include \ $ (Cocos2d)/CocosDenshion/android \ $ (Cocos2d)/extensions \ $ (Cocos2d) /extensions/GUI/CCControlExtension \ LOCAL_STATIC_LIBRARIES: = libRakNet labels: = cocos2dx_static functions cocos_extension_static include $ (BUILD_SHARED_LIBRARY) $ (call import-module, CocosDenshion/android) \ $ (call import-module, cocos2dx) \ $ (call import-module, extensions)

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.