[Cocos2d-js official documentation] XXIV how to use js on the android platform directly call Java methods

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 callStaticMethodprocess, 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.javascriptwrote a package the following Testcategories:

  1.  
    package org.cocos2dx.javascript;
  2.  
     
  3.  
    public class Test {
  4.  
     
  5.  
    public static void hello(String msg){
  6.  
    System.out.println(msg);
  7.  
    }
  8.  
     
  9.  
    public static int sum(int a, int b){
  10.  
    return a + b;
  11.  
    }
  12.  
     
  13.  
    public static int sum(int a){
  14.  
    return a + 2;
  15.  
    }
  16.  
     
  17.  
    }

So the full class name of the Test class should org/cocos2dx/javascript/Testnote 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)VA parameter representing a int, no return value of the method
  • (I)IA parameter representing a int, int return value of the method
  • (IF)ZInt 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:

  1.  
    // method call hello
  2.  
    jsb.reflection.callStaticMethod( "org/cocos2dx/javascript/Test", "hello", "(Ljava/lang/String;)V", "this is a message from js");
  3.  
     
  4.  
    // call the first sum method
  5.  
    var result = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/Test", "sum", "(II)I", 3, 7);
  6.  
    cc.log(result); //10
  7.  
     
  8.  
    // calls the second sum method
  9.  
    var result = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/Test", "sum", "(I)I", 3);
  10.  
    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.

  1.  
    // we are familiar with AppActivity to add a little something like
  2.  
    public class AppActivity extends Cocos2dxActivity {
  3.  
     
  4.  
    private static AppActivity app = null;
  5.  
    @Override
  6.  
    public void onCreate(Bundle savedInstanceState) {
  7.  
    super.onCreate(savedInstanceState);
  8.  
    app = this;
  9.  
    }
  10.  
     
  11.  
    public static void showAlertDialog(final String title,final String message) {
  12.  
     
  13.  
    // Here we must use runOnUiThread
  14.  
    app.runOnUiThread( new Runnable() {
  15.  
    @Override
  16.  
    public void run() {
  17.  
    AlertDialog alertDialog = new AlertDialog.Builder(app).create();
  18.  
    alertDialog.setTitle(title);
  19.  
    alertDialog.setMessage(message);
  20.  
    alertDialog.setIcon(R.drawable.icon);
  21.  
    alertDialog.show();
  22.  
    }
  23.  
    });
  24.  
    }
  25.  
    }
  26.  
     

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 evalStringmethod can be performed js code, which is located in frameworks\js-bindings\bindings\manual\platform\android\java\src\org\cocos2dx\libfolder. 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.

  1.  
    alertDialog.setButton( "OK", new DialogInterface.OnClickListener() {
  2.  
    public void onClick(DialogInterface dialog, int which) {
  3.  
    // must be executed in GL thread
  4.  
    app.runOnGLThread( new Runnable() {
  5.  
    @Override
  6.  
    public void run() {
  7.  
    Cocos2dxJavascriptJavaBridge.evalString( "cc.log(\"Javascript Java bridge!\")");
  8.  
    }
  9.  
    });
  10.  
    }
  11.  
    });

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

Guess you like

Origin www.cnblogs.com/wodehao0808/p/11929650.html