Adding a new feature in cocos2d-js 3.0beta, the static method java we can directly call the js by reflection on the android platform. It's very simple to use:
var o = jsb.reflection.callStaticMethod(className, methodName, methodSignature, parameters...)
In the callStaticMethod
process, we, the method name, signature Java class name by passing parameters to Java static methods can be called directly, and can get Java method's return value. Class name and signature methods described below may be a little strange, but so is the Java specification.
The class name
Parameter name must contain the full class name of the Java class package path, for example, we org.cocos2dx.javascript
wrote a package the following Test
categories:
-
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 org/cocos2dx/javascript/Test
note here must slash /
instead of Java code point in our habits .
.
Method name
The method is very simple name, is the way the original name, such as the name sum approach is sum
.
The method of signature
The method of signature a little bit complicated, the easiest method signature is ()V
, it does not represent a method parameter values did not return. Some other examples:
(I)V
A parameter representing a int, no return value of the method(I)I
A parameter representing a int, int return value of the method(IF)Z
Int and a parameter representing a float, the method returns a boolean value
There are some understanding of it, symbols in parentheses indicate the parameter type, the latter type of bracket notation represents the return value. Because Java is allowed to function overloading, you can have multiple methods were the same parameters but returns a different value method, the method signature is the same name used to help distinguish between these methods.
Currently Cocos2d-js supported Java type signature has the following four kinds:
Java type | signature |
---|---|
int | I |
float | F |
boolean | WITH |
String | Ljava/lang/String; |
parameter
Parameter may be 0 or any number, js used directly in number, bool and string can.
Examples of Use
We will call the static method above Test class:
-
// method call hello
-
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
-
-
// calls the second sum method
-
var result = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/Test", "sum", "(I)I", 3);
-
cc.log(result); //5
In your console will correct output, it is very simple.
note
Another point to note is that, in the android application, rendering and js logic cocos is carried out in gl thread, while the android itself UI update is carried out in the ui thread app, so if we call the js Java method has nothing to refresh the UI, need to be in the ui thread.
For example, in the following example we will call a Java method, it pops up a dialog box android's Alert.
-
// we are familiar with AppActivity to add a little something like
-
public class AppActivity extends Cocos2dxActivity {
-
-
private static AppActivity app = null;
-
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
app = this;
-
}
-
-
public static void showAlertDialog(final String title,final String message) {
-
-
// Here we must use runOnUiThread
-
app.runOnUiThread( new Runnable() {
-
-
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 in the js
jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "showAlertDialog", "(Ljava/lang/String;Ljava/lang/String;)V", "title", "hahahahha");
So you can call to see a native android Alert dialog of.
Re-Additional fee
Now we can call Java from js, then can not turn? of course can! Included in your project Cocos2dxJavascriptJavaBridge, this class has a evalString
method can be performed js code, which is located in frameworks\js-bindings\bindings\manual\platform\android\java\src\org\cocos2dx\lib
folder. We will add a button to just Alert dialog and execute js in its response. And contrary to the above case, the js code execution must be carried out in gl thread.
-
alertDialog.setButton( "OK", new DialogInterface.OnClickListener() {
-
public void onClick(DialogInterface dialog, int which) {
-
// must be executed in GL thread
-
app.runOnGLThread( new Runnable() {
-
-
public void run() {
-
Cocos2dxJavascriptJavaBridge.evalString( "cc.log(\"Javascript Java bridge!\")");
-
}
-
});
-
}
-
});
So after clicking the OK button, you should see the correct output in the console. evalString can execute any js code, and it has access to objects in your js code.
Reprinted from: https: //blog.csdn.net/qinning199/article/details/42045833