android java 網絡檢測

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




調用方法: get

ConnectionDetector cd = new ConnectionDetector(context); it

            boolean isInternetPresent = cd.isConnectingToInternet();             if (!isInternetPresent)             {                 Log.e(TAG, "network false !!!");                 return false;             }             else             {                 Log.e(TAG, "network is OK !!!");             }
相關文章
相關標籤/搜索