jsb.reflection.callStaticMethod无效

时间:2014-12-20 15:39:05

标签: android cocos2d-x cocos2d-x-3.0 cocos2d-js

我正试图在this tutorial之后调用JS到Java。这是javascript代码:

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

接下来是Static Java Function返回字符串

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();
}

它不起作用,没有任何错误,但是当我评论这一行时:

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

一切正常。

从JS <​​/ p>调用Java函数的上述步骤中我缺少什么

1 个答案:

答案 0 :(得分:1)

我正在做的愚蠢的错误,我已经实现了没有参数的Java函数,但我使用void参数调用函数。

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

should be ()Ljava/lang/String;"