androidcocos2d-xcocos2d-x-3.0cocos2d-js

jsb.reflection.callStaticMethod not working


I am trying to make a call from JS to Java , following this tutorial. Here is the javascript code:

var r = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity",
                                        "getUserNames", "(V)Ljava/lang/String;");
cc.log("Check this out" + r);

Next is Static Java Function to return a string

package org.cocos2dx.javascript;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.WindowManager;
import org.cocos2dx.plugin.PluginWrapper;
import org.cocos2dx.plugin.FacebookWrapper;
import android.content.Intent;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.util.Log;

// The name of .so is specified in AndroidMenifest.xml. NativityActivity will load it automatically for you.
// You can use "System.loadLibrary()" to load other .so files.

public class AppActivity extends Cocos2dxActivity{

static String hostIPAdress = "0.0.0.0";
    // Get the instance of TelephonyManager
TelephonyManager tm;

// Calling the methods of TelephonyManager the returns the information
static String IMEINumber;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    if(nativeIsLandScape()) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
    } else {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
    }
    if(nativeIsDebug()){
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }
    hostIPAdress = getHostIpAddress();
    IMEINumber = retrieveUserName();
}

@Override
public Cocos2dxGLSurfaceView onCreateView() {
    Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
    // TestCpp should create stencil buffer
    glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);

    PluginWrapper.init(this);
    PluginWrapper.setGLSurfaceView(glSurfaceView);
    FacebookWrapper.onCreate(this);

    return glSurfaceView;
}

public String getHostIpAddress() {
    WifiManager wifiMgr = (WifiManager) getSystemService(WIFI_SERVICE);
    WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
    int ip = wifiInfo.getIpAddress();
    return ((ip & 0xFF) + "." + ((ip >>>= 8) & 0xFF) + "." + ((ip >>>= 8) & 0xFF) + "." + ((ip >>>= 8) & 0xFF));
}

public String retrieveUserName(){

    tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    String im = tm.getDeviceId();
    if (im == "") {
        im = Secure.getString(getContentResolver(),
                Secure.ANDROID_ID);
    }

    return im;
}

public static String getLocalIpAddress() {
    return hostIPAdress;
}
public static String getUserNames() {
    return IMEINumber;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    FacebookWrapper.onAcitivityResult(requestCode, resultCode, data);
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    FacebookWrapper.onSaveInstanceState(outState);
}

private static native boolean nativeIsLandScape();
private static native boolean nativeIsDebug();
}

Its not working , no error nothing but when I comment this line:

var r = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity",
                                        "getUserNames", "(V)Ljava/lang/String;");

everything works properly.

What am I missing in the above steps to call Java function from JS


Solution

  • silly mistake I was doing , I have implemented Java function with no parameters but I am calling function with void parameter.

    The code here
    (V)Ljava/lang/String;" 
    
    should be ()Ljava/lang/String;"