Cocos2d-x: Interaction Between cocos2d-x and Java layer through Jni

Source: Internet
Author: User

Today's research Cocos2d-x, whim want to do a 2dx call Android pop-up box effect. Use jni interaction with wood.

C ++ calls Java and Java calls C ++ (see below)

Void HelloWorld: menuCloseCallback (CCObject * pSender) {JniMethodInfo jmi; if (JniHelper: getStaticMethodInfo (jmi, "com/jni/test/JniTest", "sayHello ", "([Ljava/lang/String;) V") {jclass str_cls = jmi. env-> FindClass ("java/lang/String"); jstring str1 = jmi. env-> NewStringUTF ("I'm a titile"); jstring str2 = jmi. env-> NewStringUTF ("Are yor exit game? "); JobjectArray arrs = jmi. env-> NewObjectArray (2, str_cls, 0); jmi. env-> SetObjectArrayElement (arrs, 0, str1); jmi. env-> SetObjectArrayElement (arrs, 1, str2); jmi. env-> CallStaticVoidMethod (jmi. classID, jmi. methodID, arrs) ;}} extern "C" {/* naming rule: Java_Java package name_class name */void Java_com_jni_test_JniTest_sayHello () {CCLog ("hello java, i'm c ");}}
/*************************************** * *********************************** Copyright (c) 2010-2012 cocos2d-x.org http://www.cocos2d-x.orgPermission Is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be encoded inall copies or substantial portions of the Software. the software is provided "as is", without warranty of any kind, express orimplied, including but not limited to the warranties of merchantability, fitness for a particle purpose and noninfringement. in no event shall theauthors or copyright holders be liable for any claim, damages or otherliability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings inthe software. **************************************** * **********************************/package com. jni. test; import java. io. unsupportedEncodingException; import org. cocos2dx. lib. cocos2dxActivity; import android. app. alertDialog; import android. app. dialog; import android. content. dialogInterface; import android. OS. bundle; import android. OS. handler; import android. OS. message; public class JniTest extends Cocos2dxActivity {private static AlertDialog mDialog = null; private static Handler mHandler = new Handler (new Handler. callback () {@ Override public boolean handleMessage (Message msg) {String [] str = (String []) msg. obj; mDialog. setTitle (str [0]); mDialog. setMessage (str [1]); mDialog. show (); return true ;}}); public static native void sayHello (); protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); mDialog = new AlertDialog. builder (this ). create (); mDialog. setButton ("OK", new AlertDialog. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {JniTest. this. finish () ;}}); mDialog. setButton2 ("cancel", new AlertDialog. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {}}); mDialog. setButton3 ("HelloC", new AlertDialog. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {// Java calls c code JniTest. this. sayHello () ;}}) ;}static {System. loadLibrary ("game");} public static void sayHello (String [] str) {Message mes = new Message (); mes. obj = str; mHandler. sendMessage (mes );}}

The effect is as follows: C ++ calls the Android dialog box
650) this. width = 650; "class =" alignnone size-full wp-image-883 max_width_img "alt =" Cocos2d-x cocos2d-x interaction with Java layer Jni "src =" http://www.bkjia.com/uploads/allimg/131228/0A232JK-0.png "style =" border: none; max-width: 100%; "/>
Java calls C ++ to print strings
650) this. width = 650; "class =" alignnone size-full wp-image-881 max_width_img "alt =" Cocos2d-x cocos2d-x interaction with Java layer Jni "src =" http://www.bkjia.com/uploads/allimg/131228/0A2324609-1.png "style =" border: none; max-width: 100%; "/>

Code address (due to time, the Code is not optimized, please refer to it ...):

Click to download sample code


More Cocos2d-x development Blog at Koyoter's Blog original title: Cocos2d-x] cocos2d-x and Java layer through Jni interactive original address: http://acoder.me/cocos2d-x-cocos-and-java-chat.html


This article is from the "one programmer" blog. For more information, contact the author!

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.