我正在尝试创建一种方法来检查需要上下文参数的互联网连接。 JNIHelper 允许我调用带参数的静态函数,但我不知道如何“检索”Cocos2d-x Activity 类以将其用作参数。
public static boolean isNetworkAvailable(Context context) {
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(
Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;
}
return haveConnectedWifi || haveConnectedMobile;
}
C++代码是
JniMethodInfo methodInfo;
if ( !JniHelper::getStaticMethodInfo( methodInfo,
"my/app/TestApp", "isNetworkAvailable", "(Landroid/content/Context;)Z")) {
//error
return;
}
CCLog( "Method found and loaded!");
methodInfo.env->CallStaticBooleanMethod( methodInfo.classID,
methodInfo.methodID);
methodInfo.env->DeleteLocalRef( methodInfo.classID);
最佳答案
Cocos2dxActivity.java:
将这一行添加到 Cocos2dxActivity:private static Activity me = null;
从 onCreate 中删除这一行:
Cocos2dxActivity.context = getApplicationContext();
在它的位置放:me = this;
使用:
(ConnectivityManager) me.getSystemService(
Context.CONNECTIVITY_SERVICE);
现在您不需要从您的 Jni 传递上下文...我知道这不是解决方案,但对于您的情况,您无需担心来自 Jni 的上下文。您可以简单地完成您的工作。
希望这有帮助.. 我在我的游戏中使用这种方式从 android 发送邮件。 :)
关于android - 使用 JNI 将 android.content.Context 参数发送到函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10264701/