-3

I want my app to be online all the time. For this, I am implementing a thread inside a service, the thread checks for internet repeatedly after every 5 seconds. So what is the best way to do this ? I want my code to be able to run in background all the time and be easy on the resources. I have tried a thread inside a service but it makes my device lag.

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;
}
  • I don't see a reason for the down vote. Also you have to provide a reason before down voting. – user7892106 Apr 21 '17 at 11:03
  • 3
    I am not one of the down voters but your comment is false. You do not HAVE to provide a reason before down voting. I would consider it nice but it is definitely not mandatory. – jotasi Apr 21 '17 at 11:14
  • 1
    you should not ask opinion-based questions here. That is a reason of downvote. There is no common *bestness* score of each method, there only is the method, which is best for **somebody's opinion**. – Vladyslav Matviienko Apr 21 '17 at 11:17
  • 1
    I'm not a down-voter either, but it's probably because continuously polling to see if you're connected is generally a bad idea (it can fail a millisecond after the last test, and it's generally better to just try to communicate and respond appropriately if the connection isn't there). If you think you have a valid reason for wanting to poll instead, you should probably add that to the question. – TripeHound Apr 21 '17 at 11:18
  • Can we try the question a different way - why do you want to know this? If you need to try to connect to the internet at some point in code, you'll find out it's not connected then, so why do you need to be checking in between - when you don't need it? – doctorlove Apr 21 '17 at 11:57

2 Answers2

0

every few seconds

None. Pooling is BAD. You got ConnectivityManager that provide callbacks to be informed about network status change.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • I know it does. But it ONLY checks for internet connection. Not for internet connectivity. Both terms are different and should not be confused. – user7892106 Apr 21 '17 at 11:06
  • Any pooling is bad usually. There's NO benefit from doing poll when you can be notified **when** the change occurs by ConnectivityManager – Marcin Orlowski Apr 21 '17 at 12:35
-1

you can use this code

public boolean isInternetAvailable() {
    try {
        InetAddress ipAddr = InetAddress.getByName("google.com"); //You can replace it with your name
        return !ipAddr.equals("");

    } catch (Exception e) {
        return false;
    }

}

click here for more detail

How to use :

if (isInternetAvailable()){
      // ... put your code here
}else{
      // ... put your code here
}
Community
  • 1
  • 1
Kingball
  • 39
  • 2