Wish I could do a test to verify internet connection, I don't want check network state, because it only detects if I have activated internet on my device, y yo quiero revisar si es posible conectarse a internet. Something like a ping.
Asked
Active
Viewed 8,557 times
5
-
See shortest way to detect Internet connection it here http://stackoverflow.com/questions/9570237/android-check-internet-connection/24692766#24692766 – Zar E Ahmer Jul 11 '14 at 07:57
-
@Nepster that doesn't fulfills the purpose of the question – therealprashant Mar 18 '15 at 05:08
3 Answers
11
Try following:
public boolean checkOnlineState() {
ConnectivityManager CManager =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo NInfo = CManager.getActiveNetworkInfo();
if (NInfo != null && NInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
dont forget the access
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
else
if (InetAddress.getByName("www.xy.com").isReachable(timeout))
{ }
else
{ }

Oli
- 3,496
- 23
- 32
-
Thats works fine, but this only check if I have Activated my wifi, 3g, but I want to check if exist internet connection, eg. sometimes we connect a wifi and the internet is down and this check if i have turned on my wifi. – Loreln May 15 '13 at 21:28
-
ok than you should use: if (InetAddress.getByName("google.com").isReachable(2000)) – Oli May 15 '13 at 21:36
-
isConnectedOrConnecting is deprecated in API level 28. Apps should instead use the ConnectivityManager.NetworkCallback API to learn about connectivity changes. ConnectivityManager.registerDefaultNetworkCallback(ConnectivityManager.NetworkCallback) and ConnectivityManager.registerNetworkCallback(NetworkRequest, PendingIntent). These will give a more accurate picture of the connectivity state of the device and let apps react more easily and quickly to changes. – Christian Oct 16 '18 at 12:27
1
It does works for me:
To verify network availability:
private Boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
}
To verify internet access:
public Boolean isOnline() {
try {
Process p1 = java.lang.Runtime.getRuntime().exec("ping -c 1 www.google.com");
int returnVal = p1.waitFor();
boolean reachable = (returnVal==0);
return reachable;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}

Musculaa
- 934
- 10
- 19
-
Your method to verify inernet acces worked for me... Except for Android 4.3 jellybean, I dont know why. – Josh Nov 19 '15 at 10:15
0
Use This code to check internet connection, it check all the internet connection over device. And Make Sure you have added Internet Permission in menifest.
boolean flag=false;
ConnectivityManager connectivity = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
flag=true;
}
}
if(flag==true)
{
Log.e("TAG","Internet Is Connected");
}
else
{
Log.e("TAG","Internet Is Not Connected");
}

Prashant Bhoir
- 900
- 6
- 8
-
`getAllNetworkInfo()` requires the ACCESS_NETWORK_STATE permission. – Lorne Laliberte May 13 '15 at 21:24
-
This method will not detect if the Wifi hotspot to which you are connecting also has internet. – Josh Nov 19 '15 at 10:16