【问题标题】:Checking internet data receiving or not (Android) [duplicate]检查互联网数据是否接收(Android)[重复]
【发布时间】:2015-12-25 10:39:01
【问题描述】:

我正在使用以下代码检查互联网连接是否可用,如果手机禁用 wifi 或数据,此代码运行良好,但问题是当互联网连接期间未接收数据时此代码挂起手机....

public class ConnectionDetector {

private Context _context;

public ConnectionDetector(Context context){
    this._context = context;
}

public boolean isConnectingToInternet(){
    ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
      if (connectivity != null) 
      {
          NetworkInfo[] info = connectivity.getAllNetworkInfo();
          if (info != null) 
              for (int i = 0; i < info.length; i++) 
                  if (info[i].getState() == NetworkInfo.State.CONNECTED)
                  {
                      return true;
                  }

      }
      return false;
}

}

【问题讨论】:

    标签: java android connection


    【解决方案1】:
      <!--Constants.INTERNET_CONNECTION_URL="YOUR_WEB_SERVICE_URL/URL OF GOOGLE";-->
    
        import android.content.Context;
        import android.net.ConnectivityManager;
        import android.net.NetworkInfo;
    
        import com.vgheater.util.Constants;
    
        import java.io.IOException;
        import java.net.HttpURLConnection;
        import java.net.URL;
    
        public class CheckConnectivity {
            private Context _context;
    
            public CheckConnectivity(Context context) {
                this._context = context;
            }
    
            public boolean isConnectingToInternet() {
                ConnectivityManager connectivity = (ConnectivityManager) _context
                        .getSystemService(Context.CONNECTIVITY_SERVICE);
                if (connectivity != null) {
                    NetworkInfo[] info = connectivity.getAllNetworkInfo();
                    if (info != null)
                        for (int i = 0; i < info.length; i++)
                            if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                                return true;
                            }
    
                }
                return false;
            }
    
            public boolean hasActiveInternetConnection() {
                if (isConnectingToInternet()) {
                    try {
                        HttpURLConnection urlc = (HttpURLConnection) (new URL(Constants.INTERNET_CONNECTION_URL).openConnection());
                        urlc.setRequestProperty("User-Agent", "Test");
                        urlc.setRequestProperty("Connection", "close");
                        urlc.setConnectTimeout(5000);
                        urlc.connect();
                        return (urlc.getResponseCode() == 200);
                    } catch (IOException e) {
                        return false;
                    }
                } else {
                    return false;
                }
            }
        }
    

    可以如下使用..

    private class InternetTask extends AsyncTask<String, Void, Boolean> {
    
          private ProgressDialog internetDialog = null;
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                internetDialog = new ProgressDialog(RegisterUser.this);
                internetDialog.setCancelable(false);
                internetDialog.setCanceledOnTouchOutside(false);
                internetDialog.setMessage(Html.fromHtml("<font  color='#616161'>Checking Internet Connectivity.</font>"));
                internetDialog.show();
            }
    
            protected Boolean doInBackground(String... urls) {
                boolean response = false;
                try {
                    response = checkConnectivity.hasActiveInternetConnection();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return response;
            }
    
            protected void onPostExecute(Boolean result) {
                internetDialog.dismiss();
                if (result) {
                    //do stuff
                } else {
                    new ShowToast(RegisterUser.this, "Connect to Internet & Try Again !!");
    
    
                }
            }
    
        }
    

    【讨论】:

    • 你的意思是在 hasActiveInternetConnection() 中返回两个语句 false ?????
    • 如果您有来自return (urlc.getResponseCode() == 200);的互联网连接,它将返回true
    • 您没有正确理解我的问题,我想检查是否在连接的 Internet 状态下数据正在接收......当我点击发布时,您的代码我的应用程序卡在按钮上数据
    • 在后台线程中尝试...如果您连接到互联网,它将尝试使用 HttpURLConnection 与指定的 URL 进行通信,如果它不会从特定的响应中得到任何响应,它将返回 false 即,你没有收到的数据..
    • 对不起先生,但是当数据没有接收到时,这种方法需要太多时间..我想快速检查是否接收到互联网数据
    猜你喜欢
    • 2017-11-30
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多