2015-09-28 27 views
-1

我正在使用下面的代碼來檢查互聯網連接是否可用,如果wifi或數據禁用移動設備,此代碼運行良好,但問題是此代碼在數據不是連接互聯網時收到....檢查互聯網數據接收與否(Android)

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; 
} 

}

回答

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 !!"); 


      } 
     } 

    } 
+0

你是什麼意思在hasActiveInternetConnection()返回兩個語句false? –

+0

它會返回true,如果你有從互聯網連接線'返回(urlc.getResponseCode()== 200);' –

+0

你沒有正確理解我的問題,我想檢查是否在互聯網狀態數據接收或不是.....用你的代碼我的應用卡在按鈕上時,我點擊發布數據 –