Witam
Czy możecie podpowiedzieć jak sprawdzać co kilka sekund czy jestem online.
Obecnie wykonuje ping do urządzenia w LAN i na tej podstawie sprawdzam czy jest połączenie ale jednorazowo np.:
if (isConnected()) {
if (ping()) {
Online.setText("Online");
i ping
public static boolean ping() {
String result = null;
try {
String ip = "192.168.0.2"; // ping address
Process p = Runtime.getRuntime().exec("/system/bin/ping -c 1 -w 10 " + ip); // ping 3 times
InputStream input = p.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(input));
StringBuffer stringBuffer = new StringBuffer();
String content = "";
while ((content = in.readLine()) != null) {
stringBuffer.append(content);
}
// ping's situation
int status = p.waitFor();
if (status == 0) {
result = "success";
return true;
} else {
result = "failed";
}
} catch (IOException e) {
result = "IOException";
} catch (InterruptedException e) {
result = "InterruptedException";
} finally {
Log.d("Ping : ", "" + result);
}
return false;
}