How to check Wifi is connected, but no Internet access in Android
12,937
Solution 1
You could try something like this:
public void checkOnlineState() {
ConnectivityManager CManager =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo NInfo = CManager.getActiveNetworkInfo();
if (NInfo != null && NInfo.isConnectedOrConnecting()) {
if (InetAddress.getByName("www.xy.com").isReachable(timeout))
{
// host reachable
}
else
{
// host not reachable
}
}
return;
}
dont forget the access
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Hope it will work :)
Solution 2
Use this :
public static boolean isInternetOn(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
// test for connection
if (cm.getActiveNetworkInfo() != null
&& cm.getActiveNetworkInfo().isAvailable()
&& cm.getActiveNetworkInfo().isConnected()) {
Log.v(TAG, "Internet is working");
// txt_status.setText("Internet is working");
return true;
} else {
// txt_status.setText("Internet Connection Not Present");
Log.v(TAG, "Internet Connection Not Present");
return false;
}
}
Hope this helps.
Solution 3
In addition to what you are doing right now,you can use BroadcastReceiver
for your application to get notified whenever the connectivity changes by registering <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
intent.
Have a look at docs: BroadcastReceiver and Connectivity Monitoring for detailed description.
I hope it will be helpful !
Related videos on Youtube
Author by
Smit Patel
I am working as a Project Manager of Mobile, WordPress, and Design Department in a software company and I am here to share my knowledge with you guys.
Updated on September 15, 2022Comments
-
Smit Patel over 1 year
I would like to know why wifi is connected but there is no internet access in Android. How can i check it? My code is:
ConnectivityManager cn=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo nf=cn.getActiveNetworkInfo(); if(nf != null && nf.isConnected() ) { Flag2=false; Log.e("network--------", "1--------------"); if (cn.getActiveNetworkInfo().isConnectedOrConnecting()) {Log.e("network--------", "11111111111111--------------"); } else {Log.e("network--------", "2222222222222--------------"); } } else { Log.e("network--------", "2--------------"); }
-
ldam about 11 years@Sam that's nit picking, it will work fine with the
== true
. -
Smit Patel about 11 yearsnot works for me @CRUSADER
-
-
ldam about 11 yearsI think the only real way to see if the internet is working is to actually try access it, like this answer suggests.
-
Smit Patel about 11 yearsThanks for the help but I want to apply this on every second and I don't wanna to lose more internet data of the user anyway thanks a lot.
-
Smit Patel about 11 yearsI used this with Async method
-
Gaurav Arora over 9 years@Oil It is giving UnknownHostException - even when the WIFI is connected (with NO INTERNET ACCESS). How to solve the same ?
-
venkat over 7 years@Smit Patel can you explain how you did in Async method? please post snippet for that.
-
Madhan about 6 yearsReceived NetworkOnMainThreadException. So, it will not be helpful for checking internet connection synchronously.
-
Stephen Hechio over 2 yearsThere is no isReachable() for Kotlin, is there a Kotlin implementation of the same?