I am trying to create a method that checks for internet connection that needs a Context parameter. The JNIHelper allows me to call static functions with parameters, but I don't know how to "retrieve" Cocos2d-x Activity class to use it as a parameter.
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;
}
and the c++ code is
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:
Add this line to Cocos2dxActivity: private static Activity me = null;
Remove this line from onCreate:
Cocos2dxActivity.context = getApplicationContext();
In its place put: me = this;
use :
(ConnectivityManager) me.getSystemService(
Context.CONNECTIVITY_SERVICE);
Now you don't need to pass the context from your Jni... I know this is not the solution but for your case you don't need to worry about context from Jni.. You can simply do your work.
Hope this helps.. I used this way to send mail from android in my game. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With