我正在尝试创建一个检查网络连接的方法,需要一个Context参数。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);