2

我正在尝试按照本教程从 JS 调用 Java 。这是javascript代码:

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

接下来是返回字符串的静态 Java 函数

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 调用 Java 函数的步骤中遗漏了什么

4

1 回答 1

1

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

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

should be ()Ljava/lang/String;"
于 2014-12-21T05:43:55.770 回答