I'm trying to get the moment where user connects to a network, then I thought a BroadcastReceiver
is a good approach... The thing that I would like to do is, when user connects to a network know if this network has connection to Internet.
The main thing also is know if the WiFi requires Browse Log in
Example : Handling Network Sign-On
documentation
What I've tried so far?
I've changed my BroadcastReceiver
to this
if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if (networkInfo != null && networkInfo.getDetailedState() == NetworkInfo.DetailedState.CONNECTED) {
Log.d("Network", "Internet YAY");
} else if (networkInfo != null && networkInfo.getDetailedState() == NetworkInfo.DetailedState.DISCONNECTED) {
if (isNetworkOnline()) {
Log.d(TAG, String.valueOf(tikis));
NetTask TeInternet = new NetTask();
TeInternet.execute("https://www.google.com");
}
}
The problem is that when I try to connect to a WiFi
without Internet Connection
the input is this :
D/Network﹕ Internet YAY
D/Network﹕ Internet YAY
D/Network﹕ Internet YAY
D/RequiresLoginBroadcast﹕ 1 //this occurs sometimes
I've changed the Inner Class
to this acording with the Handling Network Sign-On
documentation
doInBackground()
method:
protected Boolean doInBackground(String...params) {
boolean internetAccessExists = false;
String urlToBeAccessed = params[0];
final int TIMEOUT_VALUE_IN_MILLISECONDS = 15000;
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(urlToBeAccessed);
urlConnection = (HttpURLConnection) url.openConnection();
//set the respective timeouts for the connection
urlConnection.setConnectTimeout(TIMEOUT_VALUE_IN_MILLISECONDS);
urlConnection.setReadTimeout(TIMEOUT_VALUE_IN_MILLISECONDS);
//the redirect check is valid only after the response headers have been received
//this is triggered by the getInputStream() method
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
if (!url.getHost().equals(urlConnection.getURL().getHost())) {
internetAccessExists = true;
}
}
//any sort of exception is considered as a redirect.
//more specific exceptions such as SocketTimeOutException and IOException can be handled as well
catch (Exception e) {
Log.d(TAG, e.toString());
} finally {
Log.d(TAG, "Finally");
urlConnection.disconnect();
}
return internetAccessExists;
What I've looked for so far?
- How to detect when WIFI Connection has been established in Android?
- Android WIFI How To Detect When Specific WIFI Connection is Available
- BroadcastReceiver is not working (detect if wifi is connected)
And more... but saddly I didn't find the correct answer to me.
TL;DR
The thing that I'm trying to do is get the exact event that users connects to a network and then get a good method to detect if I can make a google ping or to check if is there connection to Internet (ONLY WITH WIFI, 3G CONNECTION IS NOT ALLOWED), because the code that I'm using at the moment is failing sometimes...
I think this is a good method to know if there is an Internet Connection
since the thing that I want to know is detect if Wifi Requires Browser Login.
We are almost done... But I don't get why is entering on the BroadcastReceiver
4 times or even 5.... and sometimes saying that there's Internet connection
when there is not...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…