Executes single ping and return - Android Network

Android examples for Network:Ping

Description

Executes single ping and return

Demo Code


//package com.java2s;
import android.util.Log;

public class Main {
    private final static String TAG = "ICMPUtil";

    /**/*from  ww w .j ava2s .  c  o  m*/
     * Executes single ping and return
     * @param ip - address to ping
     * @return true - ping was successful
     */
    public static boolean ping(String ip) {
        Runtime runtime = Runtime.getRuntime();

        try {
            Process process = runtime.exec("/system/bin/ping -c 1 " + ip);
            return process.waitFor() == 0;

        } catch (Exception ignore) {
            Log.e(TAG, "Error while", ignore);
        }

        return false;
    }
}

Related Tutorials