我希望在互联网再次启动时将我的嵌入式数据库与我的服务器同步,有一种干净的方法可以在后台线程中用一段时间(真实)检查互联网连接吗?
现在我正在以这种方式手动检查互联网连接:
public boolean isOnline() {
Runtime runtime = Runtime.getRuntime();
try {
Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
int exitValue = ipProcess.waitFor();
return (exitValue == 0);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return false;
}
我解决了这个问题: Internet listener Android example