Is Ping OK - Android Network

Android examples for Network:Ping

Description

Is Ping OK

Demo Code


//package com.java2s;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static boolean isPingOk(String ip) {
        try {//ww w .j  a v a  2  s.c  o  m
            Process p = Runtime.getRuntime().exec(
                    "/system/bin/ping -c 10 -w 4 " + ip);
            if (p == null) {
                return false;
            }

            BufferedReader in = new BufferedReader(new InputStreamReader(
                    p.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                if (line.contains("bytes from")) {
                    return true;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return false;
    }
}

Related Tutorials