I have been doing some research and seen some debate of whether to use Broadcast Receiver or Alarm Manager, Im not sure what to use but here is what I am trying to do and have done.
I have a method that will check if there is internet connection. And then updates the UI accordingly.
public void isNetworkAvailable() {
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
mLayout.removeView(noInternetView);
checkInternetViewOpen = false;
} else {
if (!checkInternetViewOpen) {
checkInternetViewOpen = true;
mLayout.addView(noInternetView, params);
}
}
}
And while I am in an activity that will be using the internet I want to run this once every few seconds to make sure the internet is still active. Like this
while (usingInternet) {
//I need to make it wait
isNetworkAvailable();
}
I also need to be not on the main thread for this, so I can make it wait, and then I will adjust the updating the UI parts on the main tread.
So how can I make this on a background thread? And what option should I use?
Thanks for the help in advance.