Current location - Quotes Website - Signature design - How to use JS to directly call Java methods on Android platform
How to use JS to directly call Java methods on Android platform
Cocos2d-js 3.0beta on Android platform has added a new feature, and we can directly call Java static methods in js through reflection. The usage method is simple: in the callStaticMethod method, by passing in the class name, method name, method signature and parameters of Java, you can directly call the static method of Java and get the return value of the Java method. The class name and method signature introduced below may be a bit strange, but that is the specification of Java.

(1) class name

The class name in the parameter must be the full class name containing the path of the Java package. For example, a test class is written under the package org.cocos2dx.JavaScript:

Then the full class name of this test class should be org/cocos2dx/javascript/Test. Note that it must be a slash/,not a dot used in Java code.

(2) Method name

The method name is very simple, which is the original name of the method. For example, the name of the sum method is sum.

(3) Method signature

The method signature is a bit complicated. The simplest method signature is () v, which means a method with no parameters and no return value. Other examples:

(I)V represents a method with an int parameter and no return value;

(I)I represents a method that takes int as a parameter and takes int as a return value;

(IF)Z represents a method whose parameters are int and float, and the return value is boolean.

Now I have some understanding. The symbols in parentheses indicate the parameter type, and the symbols after parentheses indicate the return value type. Because Java allows function overloading, there can be multiple methods with the same name but different parameter return values. Method signatures are used to help distinguish these methods with the same name.

(4) parameters

The parameter can be zero or any number, just use number, bool and string in js directly.

(5) Use examples

The static method in the above test class will be called:

//Call the hello method

Jsb.reflection.call static method ("org/cocos2dx/JavaScript/test", "Hello"? "(Ljava/lang/String)V", "This? Is it? Answer? Leave a message From where? js”);

//Call the first sum method.

var? The result? =? Jsb.reflection.call static method ("org/cocos2dx/JavaScript/test", "sum",? "I", 3, 7);

Cc.log (result); ? // 10

//Call the second sum method.

var? The result? =? Jsb.reflection.call static method ("org/cocos2dx/JavaScript/test", "sum",? "(I) me"? 3);

Cc.log (result); ? //5

(6) Attention

It should also be noted that in android applications, the rendering of cocos and the logic of js are carried out in gl thread, while the ui update of android itself is carried out in the ui thread of app. Therefore, if the Java method called in js has any operation to refresh the UI, it needs to be done in the UI thread.

For example, in the following example, a Java method will be called to pop up the warning dialog box of android.

//Add something to the familiar AppActivity class.

Public? Class? AppActivity? Extension? Cocos2dxActivity? {

Private? Static electricity AppActivity? app? =? null

@ Overlay

Public? Invalid? OnCreate (bundle? savedInstanceState)? {

super . oncreate(savedInstanceState);

app? =? This;

}

Public? Static electricity Invalid? ShowAlertDialog (finally? String? Title, final? String? Message)? {

//runOnUiThread must be used here.

App.runOnUiThread (new? Runnable()? {

@ Overlay

Public? Invalid? run()? {

AlertDialog? alertDialog? =? New? Alert dialog box. The builder (app). create();

Alert dialog. caption (title);

AlertDialog.setMessage (message);

alert dialog . seticon(r . drawable . icon);

alert dialog . show();

}

});

}

}

Call in js

jsb . reflection . callstatic method(" org/cocos 2 dx/JavaScript/app activity ",“showAlertDialog”,? “(Ljava/lang/String; ljava/lang/String; )V“,? "Title"? "Ha ha ha ha");

In this way, you can see a warning dialog box that comes with android.