gpt4 book ai didi

android - jsb.reflection.callStaticMethod 不工作

转载 作者:行者123 更新时间:2023-11-29 01:36:36 25 4
gpt4 key购买 nike

我正在尝试从 JS 调用 Java,遵循 this tutorial .这是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函数的步骤我漏了什么

最佳答案

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

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

should be ()Ljava/lang/String;"

关于android - jsb.reflection.callStaticMethod 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27581720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com