I'd like to check my void that check if i'am connected to Wi-Fi. For now i have it on button click but i want to check it whole time so at the seccond i'am not connected to Wi-Fi my void will work. I was thinking about putting it in while but i don't know if thats good for program. If you know how to do that please show me how. Thanks all.
My void
private void GoToNoWifi (){
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected()) {
}else{
startActivity(new Intent(this,NoWifiConnection.class));
}
}
CodePudding user response:
Depends on when you want to know it. While loop seems to be the easiest and simplest solution.
You can use this and check every 5 seconds if there is a wifi connection or not if you don't want to do it every frame:
while (true) {
// Check for Wifi connection
// Wait 300 seconds (5 minutes) and run it again, forever
int waittime = 300;
Thread.sleep(waittime * 1000);
}