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;
}