Cocos js java interdebugging (how to use JS to directly call JAVA on the ANDROID platform)

Source: Internet
Author: User

Cocos js java interdebugging (how to use JS to directly call JAVA on the ANDROID platform)

Added a new feature in cocos2d-js 3.0beta. on android, we can call java's static method directly in js through reflection. It is easy to use:

var o = jsb.reflection.callStaticMethod(className, methodName, methodSignature, parameters...)

IncallStaticMethodMethod, we pass in the Java class name, method name, method signature, parameters can directly call the Java static method, and get the return value of the Java method. The class name and method signature described below may be a bit strange, but the Java specification is like this.

Class Name

The class name in the parameter must be the complete class name that contains the Java package path. For exampleorg.cocos2dx.javascriptThis package containsTestClass:

package org.cocos2dx.javascript;public class Test {    public static void hello(String msg){        System.out.println(msg);    }    public static int sum(int a, int b){        return a + b;    }    public static int sum(int a){        return a + 2;    }}

The complete Class Name of the Test class should beorg/cocos2dx/javascript/Test, Note that this must be a diagonal line/Instead of the point we get used to in Java code..

Method Name

The method name is simple, that is, the original name of the method. For example, the name of the sum method issum.

Method Signature

The method signature is a little complicated. The simplest method signature is()VIt indicates a method without parameters and no return value. Other examples:

  • (I)VIndicates that the parameter is an int and there is no return value.
  • (I)IIndicates the method in which the parameter is an int and the return value is an int.
  • (IF)ZIndicates a method in which the parameter is an int and a float, and the return value is boolean.

    Now I have some understanding. the symbol in the brackets indicates the parameter type, and the symbol behind the brackets indicates the type of the return value. Because Java allows function overloading, there can be multiple methods with the same name but different return values. The method signature is used to help distinguish these methods with the same name.

    Currently, the Java type signatures supported in the Cocos2d-js are as follows:

    Java type Signature
    Int I
    Float F
    Boolean Z
    String Ljava/lang/String;
    Parameters

    The parameters can be 0 or any number. You can directly use number, bool, and string in js.

    Example

    We will call the static method in the Test class above:

    // Call the hello method jsb. reflection. callStaticMethod ("org/cocos2dx/javascript/Test", "hello", "(Ljava/lang/String;) V", "this is a message from js "); // call the First sum Method var result = jsb. reflection. callStaticMethod ("org/cocos2dx/javascript/Test", "sum", "(II) I", 3, 7); cc. log (result); // 10 // call the second sum Method var result = jsb. reflection. callStaticMethod ("org/cocos2dx/javascript/Test", "sum", "(I) I", 3); cc. log (result); // 5

    There will be correct output in your console, which is simple.

    Note:

    Note that in android applications, cocos rendering and js logic are performed in the gl thread, android UI updates are performed in the app ui thread. Therefore, if the Java method we call in js has any UI refresh operations, they must be performed in the ui thread.

    For example, in the following example, we will call a Java method, which will pop up an Alert dialog box for android.

    // Add something to the familiar AppActivity class. public class AppActivity extends Cocos2dxActivity {private static AppActivity app = null; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); app = this;} public static void showAlertDialog (final String title, final String message) {// runOnUiThread app must be used here. runOnUiThread (new Runnable () {@ Override public void run () {AlertDialog alertDialog = new AlertDialog. builder (app ). create (); alertDialog. setTitle (title); alertDialog. setMessage (message); alertDialog. setIcon (R. drawable. icon); alertDialog. show ();}});}}

    Then we call

    jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "showAlertDialog", "(Ljava/lang/String;Ljava/lang/String;)V", "title", "hahahahha");

    In this way, you can see an android native Alert dialog box.

    Add more materials

    Now we can call Java from js. Can this problem be reversed? Of course!
    Your project contains Cocos2dxJavascriptJavaBridge, which hasevalStringMethod can execute js code, which is located inframeworks\js-bindings\bindings\manual\platform\android\java\src\org\cocos2dx\libFolder. We will add a button to the Alert dialog box and execute js in its response. In contrast to the above situation, this execution of js Code must be performed in the gl thread.

    AlertDialog. setButton ("OK", new DialogInterface. onClickListener () {public void onClick (DialogInterface Diener, int which) {// The app must be executed in the GL thread. runOnGLThread (new Runnable () {@ Override public void run () {Cocos2dxJavascriptJavaBridge. evalString ("cc. log (\ "Javascript Java bridge! \")");}});}});

    In this way, after clicking the OK button, you should be able to see the correct output in the console. EvalString can execute any js Code and it can access your js Code objects.

     

    Above from http://www.cocos2d-x.org/docs/manual/framework/html5/v3/reflection/zh

     

     

    Add more materials by cnmm22:

    When java calls js
    Cocos2dxJavascriptJavaBridge class not found

     

    If you use the cocos command line, you should add the index of the Cocos2dxJavascriptJavaBridge class. Search for Cocos2dxJavascriptJavaBridge. java and add it to the cocos2d. lib directory.

    Use cocos compile-p android
    Cocos2dxJavascriptJavaBridge. java in frameworks \ js-bindings \ manual \ platform \ android \ java \ src \ org \ cocos2dx \ lib
    Copy Cocos2dxJavascriptJavaBridge. java into frameworks \ js-bindings \ cocos2d-x \ cocos \ platform \ android \ java \ src \ org \ cocos2dx \ lib

    AppActivity. java
    Import org. cocos2dx. lib. Cocos2dxJavascriptJavaBridge;

    Compile succeeded.

     

    After Cocos2dxJavascriptJavaBridge is added, no error is reported, but the data in cc. log () is not output in the console during running?

    Js cannot be output in ADT.

     

Related Article

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.