/**
   * Detects network changes and resetting network when needed
   *
   * <p>Note: the intent info is only for this particular change, meaning it will get sent when the
   * phone detects changes in the wireless state even though the user is actually using wifi. so,
   * don't use it, look for the current real state using the manager service.
   */
  @Override
  public void onReceive(Context context, Intent intent) {

    if (conMgr == null) return;

    // Try WIFI data connection
    NetworkInfo wifiInfo = conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiInfo.isConnected()) {
      setHasNetwork(true, null);
      return;
    }

    // Try mobile connection
    NetworkInfo mobileInfo = conMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    // Reset network if type changed
    if (currentNetworkSubType != mobileInfo.getSubtype()) {
      currentNetworkSubType = mobileInfo.getSubtype();
      resetNetwork();
    }

    // On 2.2 this receiver fires when we register for it, so we get the current state, not a
    // transition, so
    // typically in this case isFailover() is going to be true from the last failover, so we don't
    // reset the
    // network in that case. We also have the default HttpAccess instance created early on
    // so that this initial receive can be processed before we try and make any calls
    if (mobileInfo.isFailover()) {
      if (!mobileInfo.isConnected()) {
        setHasNetwork(false, "No active data connection");
        return;
      }
    }

    if (mobileInfo.isConnected()) {
      setHasNetwork(true, null);
      return;
    }

    setHasNetwork(
        false, mobileInfo.getReason() == null ? wifiInfo.getReason() : mobileInfo.getReason());
  }
 public static boolean isNetWorkAvailable(Context context) {
   boolean isAvailable = false;
   ConnectivityManager connectivityManager =
       (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
   if (connectivityManager != null) {
     NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
     if (networkInfo != null) {
       DetailedState detailedState = networkInfo.getDetailedState();
       String detailedName = detailedState.name();
       if (detailedName == null) {
         detailedName = "";
       }
       networkTable.put("detailedName", detailedName);
       String extraInfo = networkInfo.getExtraInfo();
       if (extraInfo == null) {
         extraInfo = "";
       }
       networkTable.put("extraInfo", extraInfo);
       String reason = networkInfo.getReason();
       if (reason == null) {
         reason = "";
       }
       networkTable.put("reason", reason);
       NetworkInfo.State state = networkInfo.getState();
       String stateName = "";
       if (state != null && state.name() != null) {
         stateName = state.name();
       }
       // 经过多次测试,只有stateName可以准确的判断网络连接是否正常
       if ("CONNECTED".equalsIgnoreCase(stateName)) {
         isAvailable = true;
       }
       networkTable.put("stateName", stateName);
       int subType = networkInfo.getSubtype();
       networkTable.put("subType", subType + "");
       String subtypeName = networkInfo.getSubtypeName();
       if (subtypeName == null) {
         subtypeName = "";
       }
       networkTable.put("subtypeName", subtypeName);
       //				int type = networkInfo.getType();
       String typeName = networkInfo.getTypeName();
       if (typeName == null) {
         typeName = "";
       }
       networkTable.put("typeName", typeName);
       Log.d(LOG_TAG, getLogString());
     }
   }
   return isAvailable;
 }
Beispiel #3
0
  private String getConnectivity() {
    ConnectivityManager conn =
        (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);

    String result = "<table>";
    result += "<tr>";
    result += "<th>DetailedState</th>";
    result += "<th>ExtraInfo</th>";
    result += "<th>Reason</th>";
    result += "<th>State</th>";
    result += "<th>Subtype</th>";
    result += "<th>Subtype name</th>";
    result += "<th>Type</th>";
    result += "<th>Type name</th>";
    result += "<th>Is connected</th>";
    result += "<th>Is connected or connecting</th>";
    result += "<th>Is failover</th>";
    result += "<th>Is roaming</th>";
    result += "</tr>\n";

    for (NetworkInfo info : conn.getAllNetworkInfo()) {
      result += "<tr>";
      result += "<td>" + info.getDetailedState() + "</td>";
      result += "<td>" + info.getExtraInfo() + "</td>";
      result += "<td>" + info.getReason() + "</td>";
      result += "<td>" + info.getState() + "</td>";
      result += "<td>" + info.getSubtype() + "</td>";
      result += "<td>" + info.getSubtypeName() + "</td>";
      result += "<td>" + info.getType() + "</td>";
      result += "<td>" + info.getTypeName() + "</td>";
      result += "<td>" + info.isConnected() + "</td>";
      result += "<td>" + info.isConnectedOrConnecting() + "</td>";
      result += "<td>" + info.isFailover() + "</td>";
      result += "<td>" + info.isRoaming() + "</td>";
      result += "</tr>\n";
    }
    result += "</table>";

    return result;
  }
 /**
  * An optional reason for the connectivity state change may have been supplied. This returns it.
  *
  * @return the reason for the state change, if available, or {@code null} otherwise.
  */
 public String getReason() {
   return mNetworkInfo.getReason();
 }