android ping IP

 /**
     * =========通过ip ping 来判断ip是否通
     *
     * @param ip
     */
    private void judgeTheConnect(String ip) {

        try {

            if (ip != null) {

                //代表ping 3 次 超时时间为10秒
                Process p = Runtime.getRuntime().exec("ping -c 3 -w 10 " + ip);//ping3次

                int status = p.waitFor();

                if (status == 0) {
                    //代表成功
                    Toast.makeText(mActivity, "成功", Toast.LENGTH_SHORT).show();

                } else {
                    //代表失败
                    Toast.makeText(mActivity, "失败", Toast.LENGTH_SHORT).show();
                }
            } else {
                  //代表失败
                    Toast.makeText(mActivity, "IP为空", Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            LogUtil.e(aaa, e.getMessage());
        }

    }

你可能感兴趣的:(Android)