我想创建一个方法来检查需要Context参数的Internet连接。 JNIHelper允许我使用参数调用静态函数,但我不知道如何“检索”Cocos2d-x Activity类以将其用作参数。发送一个android.content.Context参数给JNI函数
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);
你能告诉我你为什么需要这方面?只是为了getSystemService ????? – 2012-04-24 09:54:09
这不是解决方案,但isNetworkAvailable的描述符是错误的。它应该是:“(Landroid/content/Context;)Z”。请注意L和Z – 2012-04-27 21:37:36