My server constantly checks if my android application is online. May I ask what are the ways that I can do on my android application
Asked
Active
Viewed 1.8k times
4
-
What do you mean by online? Like if it's connected to network or not? – Bidhan May 20 '15 at 07:30
-
Possible duplicate: http://stackoverflow.com/questions/1560788/how-to-check-internet-access-on-android-inetaddress-never-timeouts – Sami Eltamawy May 20 '15 at 07:31
-
We have a server and I want to check if my android application is running/online – Erwin Daez May 20 '15 at 08:55
2 Answers
21
Create a helper method called isNetworkAvailable() that will return true or false depending upon whether the network is available or not. It will look something like this
private boolean isNetworkAvailable() {
ConnectivityManager manager =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
if (networkInfo != null && networkInfo.isConnected()) {
// Network is present and connected
isAvailable = true;
}
return isAvailable;
}
Don't forget to add the following permission to your Manifest file
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Edit: The above method only checks if the network is available. It doesn't check if the device can actually connect to the internet. The best way to check if there is an Internet connection present is to try and connect to some known server using HTTP
public static boolean checkActiveInternetConnection() {
if (isNetworkAvailable()) {
try {
HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
return (urlc.getResponseCode() == 200);
} catch (IOException e) {
Log.e(LOG_TAG, "Error: ", e);
}
} else {
Log.d(LOG_TAG, "No network present");
}
return false;
}
By the way, take care not to run the above method on your Main thread or it will give your NetworkOnMainThreadException. Use an AsyncTask or something equivalent.

Bidhan
- 10,607
- 3
- 39
- 50
-
It seems that this method checks only network connection but not the internet connection. You can have a wi-fi without internet connection, for example. – Ilya Vorobiev May 20 '15 at 07:38
-
Actually what I'm trying to do is the other way around, I have my server and I want to check if my application is running/online on a particular device. BTW thanks for this information. – Erwin Daez May 20 '15 at 07:46
-
You should configure your server to send pings after some time interval to the device running your app and the device should reply back. If the device is not replying this means that it is offline. – Bidhan May 20 '15 at 07:54
-
1According to the documentation, the NetworkInfo.isConnected() method "Indicates whether network connectivity exists and it is possible to establish connections and pass data." and also that it "... guarantees that the network is fully usable." So do we really need this additional checkActiveInternetConnection() method?? Which is true? – Nerdy Bunz Jul 07 '18 at 10:26
-
NetworkInfo class was deprecated in API level 29. See https://developer.android.com/reference/android/net/NetworkInfo.html – Siarhei Kavaleuski Sep 11 '19 at 19:41
-
1Make sure to use `https` instead of `http` otherwise it wont work. – Saurabh Padwekar Mar 04 '20 at 02:40
-
with http I guess you get exception, when I use https I am getting this error and I don't know what does this mean : Error retrieving account java.lang.IllegalStateException: No current tap-and-pay account @SaurabhPadwekar you have any idea for this error ? – Badri Paudel Jan 08 '21 at 05:16
0
public boolean isOnline() {
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
if(networkInfo != null && networkInfo.isConnectedOrConnecting()){
return true;
}
else{
return false
}
}

Umanda
- 4,737
- 3
- 23
- 28