0

Newbie to Android app programming and I have a scenario that I'm struggling to code up in Android Studio.

My app requires the user be connected to a specific Wifi. So I'd like to periodically check the Wifi status and if it's not connected/ connected to the wrong Wifi, I want to throw up an AlertDialog till the user is connected to the correct Wifi.

However, I'm struggling with the implementation. My approach so far has been to have a method checkWifi() that tests if we're on the correct Wifi, and sets a global boolean onCorrectWifi accordingly. This checkWifi() runs periodically, every 30s via a TimerTask.

Within the same TimerTask as the checkWifi() method, is another method called handleWifiStatus(). The handleWifiStatus() method looks at onCorrectWifi and if it's True, does nothing. If onCorrectWifi is False, handleWifiStatus() spawns an AlertDialog and then enters a while loop. The while loop calls checkWifi() repeatedly until onCorrectWifi is True again, at which point the while loop is exited and the AlertDialog is dismissed and usual app activities resume.

I'm struggling with the actual implementation of this.

Am I making this too complicated for myself? Is there a better/ more simple implementation that'll achieve the whole "check Wifi state, if wrong, show AlertDialog till Wifi is good again" concept?

flexcookie
  • 113
  • 1
  • 3
  • 12
  • "My issues are that I can't get the AlertDialog to show" - show the relevant code, and how this is called i.e. is this in a permanently running foreground service or Activity. If this is the only issue the 4 paragraphs before it aren't really necessary? – Mark May 29 '18 at 22:59
  • @Mark Keen, apologies, I see your point. I suppose my overarching question is about the actual implementation. I'll edit my question to reflect this. – flexcookie May 29 '18 at 23:03

2 Answers2

1

At first look, your methodology seems sound, so I'm not quite sure what's going wrong. That being said, Android actually has functionality in place to be notified when the network status changes to simplify exactly this scenario.

// Retrieve the ConnectivityManager via the current Context
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);

// Create the method to be called when the WiFi network changes
ConnectivityManager.NetworkCallback callback = new ConnectivityManager.NetworkCallback() {
    @Override
    public void onAvailable(Network network) {
        // Check that this Network is the correct one and take
        // action as appropriate
    }
};

// Set the callback to be fired when WiFi status changes
cm.registerNetworkCallback(
     new NetworkRequest.Builder()
     .addTransportType(TRANSPORT_WIFI)
     .build(),
     callback
);
Elan Hamburger
  • 2,137
  • 10
  • 14
  • So just to clarify, this mitigates the need for periodically checking the Wifi status? As in, the callback will only fire when the Wifi state changes from Connected -> Disconnected and vice versa? Follow on question, where would the Dialog fit into this? – flexcookie May 30 '18 at 00:41
  • The `onAvailable` method of `callback` will run each time the WiFi is connected (or changed networks). You can optionally define another method `onLost(Network network)` which will run when the WiFi is disconnected entirely. When `onAvailable` is called, check if the `Network` is the WiFi network you want. If it's not, show the dialog, otherwise, dismiss the dialog. – Elan Hamburger May 30 '18 at 02:34
  • Excellent, thank you for your help! Saves having to mess around with other threads and whatnot. – flexcookie May 30 '18 at 03:13
0
  • I would use BroadcaseReceiver to capture the Wifi connection change event. In this way, you don't need to run the periodic check. (See: How to detect when WIFI Connection has been established in Android?)

  • Not sure what kind of Dialog you are using, but if you are using a general dialog built by AlertDialog.Builder, you don't need to run the while loop to keep displaying the dialog. Simply call dialog.create().show() to display it and dismiss it only when the right Wifi connection is established. Given the scenario, I will choose either ProgressDialog or ProgressBar instead of AlertDialog.

WasabiTea
  • 389
  • 2
  • 10