Java Android-通过ping url地址检查互联网连接

Java Android-通过ping url地址检查互联网连接,java,android,server,ping,Java,Android,Server,Ping,我需要一个在后台不断ping谷歌服务。但是我不知道怎么做。我是新来的。我的方法不起作用,不会重复。它只工作一次,并且总是返回“false” 与服务器功能断开连接 public boolean isConnectedToServer(String url, int timeout) { try{ URL myUrl = new URL(url); URLConnection connection = myUrl.openConnection(); connection.setConn

我需要一个在后台不断ping谷歌服务。但是我不知道怎么做。我是新来的。我的方法不起作用,不会重复。它只工作一次,并且总是返回“false”

与服务器功能断开连接

 public boolean isConnectedToServer(String url, int timeout) {
try{
  URL myUrl = new URL(url);
  URLConnection connection = myUrl.openConnection();
  connection.setConnectTimeout(timeout);
  connection.connect();
  return true;
} catch (Exception e) {
  // Handle your exceptions
  return false;
}}
onCreate

     @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 if(isConnectedToServer("http://www.google.com",3000)){
      Toast.makeText(this, "Okay", Toast.LENGTH_SHORT).show();
    }else{
      Toast.makeText(this, "Not Okay", Toast.LENGTH_SHORT).show();
    }}
清单

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />


我在屏幕上看到一次不好。只有一次。即使我有网络连接。对此我能做些什么?

尝试创建一个扩展AsyncTask的类

public class CheckInternet extends AsyncTask<Void, Void, Boolean>{
 private static final String TAG = "CheckInternet";
private Context context;


public CheckInternet(Context context) {
    this.context = context;

}

@Override
protected Boolean doInBackground(Void... voids) {
    Log.d(TAG, "doInBackground: ");
    ConnectivityManager cm =
            (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

    assert cm != null;
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null &&
            activeNetwork.isConnected();

    if (isConnected) {
        if ( executeCommand()) return true;
    }
    return false;
}

private boolean executeCommand(){
    System.out.println("executeCommand");
    Runtime runtime = Runtime.getRuntime();
    try
    {
        Process  mIpAddrProcess = runtime.exec("/system/bin/ping -c "+"www.google.com");
        int mExitValue = mIpAddrProcess.waitFor();
        System.out.println(" mExitValue "+mExitValue);
        if(mExitValue==0){
            return true;
        }else{
            return false;
        }
    }
    catch (InterruptedException ignore)
    {
        ignore.printStackTrace();
        System.out.println(" Exception:"+ignore);
    }
    catch (IOException e)
    {
        e.printStackTrace();
        System.out.println(" Exception:"+e);
    }
    return false;
}
公共类检查Internet扩展异步任务{
私有静态最终字符串TAG=“CheckInternet”;
私人语境;
公共支票互联网(上下文){
this.context=上下文;
}
@凌驾
受保护的布尔doInBackground(Void…voids){
Log.d(标签“doInBackground:”);
连接管理器cm=
(ConnectionManager)context.getSystemService(context.CONNECTIVITY_服务);
断言cm!=null;
NetworkInfo activeNetwork=cm.getActiveNetworkInfo();
布尔值isConnected=activeNetwork!=null&&
activeNetwork.isConnected();
如果(未连接){
if(executeCommand())返回true;
}
返回false;
}
私有布尔值executeCommand(){
System.out.println(“executeCommand”);
Runtime=Runtime.getRuntime();
尝试
{
Process mIpAddrProcess=runtime.exec(“/system/bin/ping-c”+”www.google.com”);
int-mExitValue=mIpAddrProcess.waitFor();
System.out.println(“mExitValue”+mExitValue);
如果(mExitValue==0){
返回true;
}否则{
返回false;
}
}
捕获(中断异常忽略)
{
ignore.printStackTrace();
System.out.println(“异常:+忽略”);
}
捕获(IOE异常)
{
e、 printStackTrace();
System.out.println(“异常:+e”);
}
返回false;
}

您可以做的第一件事是打印try-catch中的错误日志,这可能会给您一个提示。您可能还需要internet权限。请更新您问题的日志异常。无法解析符号“HelperMethods”,此处需要常量“Interface expected here”获取这些错误。我也看不到www.google.com中的代码。我需要ping-google.Try now。我已经更新了代码。在helper类中,我已经创建了SharedReferences方法。我已经删除了它