A new feature has been added to the Cocos2d-js 3.0beta, which allows us to invoke Java's static methods directly in JS via reflection on the Android platform. The way it's used is simple:
var o = Jsb.reflection.callStaticMethod (ClassName, MethodName, methodsignature, parameters ...)
In the callStaticMethod
method, we can call Java's static method directly by passing in Java's class name, method name, method signature, and can get the return value of Java method. The class name and method signature described below may be a little odd, but Java's specifications are the same.
Class name
parameter must be the full class name that contains the Java package path, for example we are in org.cocos2dx.javascript
This package is written under a test
class:
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; }}
So the full class name of the test class should be org/cocos2dx/javascript/Test
, note that this must be a slash /
, not the point we used to in Java code .
.
Method name
The method name is simple, that is, the method's original name, such as the name of the sum method sum
.
Method signature
The method signature is slightly more complex, and the simplest method signature is ()V
that it represents a method that does not have a parameter that does not return a value. Some other examples:
(I) V
means that the parameter is an int, there is no way to return a value
(i) I
means the parameter is an int, the method that returns the value int is
(IF) Z
means that the parameter is an int and a float, and the return value is Boolean
Now there's some understanding, the notation inside the parentheses represents the parameter type, and the symbol after the parentheses indicates the return value type. Because Java is allowed for function overloading, there can be several methods with the same method name but different parameter return values, which are used to help distinguish the methods of the same names.
The following 4 types of Java type signatures are currently supported in COCOS2D-JS:
Java Type |
Signature |
Int |
I |
Float |
F |
Boolean |
Z |
String |
ljava/lang/string; |
Parameters
Parameters can be 0 or any number, directly using the JS Number,bool and string can be.
Using the example
We will invoke the static method in the test class above:
//calls the Hello method Jsb.reflection.callStaticMethod (" Org/cocos2dx/javascript/test "," Hello "," ( ljava/lang/string;) V "," This was 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//calls the second sum method var result = Jsb.reflection.callStaticMethod ("Org/cocos2dx/javascript/test", "Sum", "(i) I", 3); Cc.log (result); 5
It's easy to have the right output on your console.
Attention
It is also important to note that in Android applications, the Cocos rendering and JS logic is carried out in the GL thread, and Android itself UI update is in the app UI thread, so if we call in JS Java method has any refresh UI operation, All need to be done on the UI thread.
For example, in the following example we call a Java method, which pops up an Android Alert dialog box.
//give us a familiar appactivity class A little something public class Appactivity extends Cocos2dxactivity {private static A Ppactivity app = null; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); App = this; } public static void Showalertdialog (final string title,final string message) {//must be used here Runonuithread AP P.runonuithread (New Runnable () {@Override public void run () {Alertdialog Alertdialo g = new Alertdialog.builder (APP). Create (); Alertdialog.settitle (title); Alertdialog.setmessage (message); Alertdialog.seticon (R.drawable.icon); Alertdialog.show (); } }); }}
And then we call it in JS.
jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "showAlertDialog", "(Ljava/lang/String;Ljava/lang/String;)V", "title", "hahahahha");
This calls you to see an Android Native Alert dialog box.
Add some more material.
Now that we can call Java from JS, can it be reversed? Of course! Include Cocos2dxjavascriptjavabridge in your project, this class has a evalstring
method can execute JS code, it is located in frameworks\js-bindings\bindings\manual\platform\android\java\src\org\cocos2dx\lib
folder. We will add a button to the Alert dialog box and execute JS in its response. In contrast to the above scenario, this execution of the JS code must be done in the GL thread.
alertdialog.setbutton (" OK ", new Dialoginterface.onclicklistener () {public void OnClick ( Dialoginterface dialog, int which) {//Be sure to execute app.runonglthread in the GL thread (new Runnable () {@Override public void Run () {cocos2dxjavascriptjavabridge.evalstring ("Cc.log (\" Javascript Java bridge!\ ")" ); } }); }});
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 object in the JS code.
Reprinted from: http://www.cocos2dx.net/post/258
"COCOS2D-JS Official Document" 24, how to use JS directly on the Android platform to call the Java method