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; }
public void saveWifiState(Bundle savedState) { savedState.putParcelable(KEY_CONFIG, mConfig); savedState.putParcelable(KEY_SCANRESULT, mScanResult); savedState.putParcelable(KEY_WIFIINFO, mInfo); if (mState != null) { savedState.putString(KEY_DETAILEDSTATE, mState.toString()); } }
static String get(Context context, String ssid, DetailedState state) { String[] formats = context .getResources() .getStringArray((ssid == null) ? R.array.wifi_status : R.array.wifi_status_with_ssid); int index = state.ordinal(); if (index >= formats.length || formats[index].length() == 0) { return null; } return String.format(formats[index], ssid); }
public static String getSummary( Context context, String ssid, DetailedState state, boolean isEphemeral, String passpointProvider) { if (state == DetailedState.CONNECTED && ssid == null) { if (TextUtils.isEmpty(passpointProvider) == false) { // Special case for connected + passpoint networks. String format = context.getString(R.string.connected_via_passpoint); return String.format(format, passpointProvider); } else if (isEphemeral) { // Special case for connected + ephemeral networks. return context.getString(R.string.connected_via_wfa); } } // Case when there is wifi connected without internet connectivity. final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (state == DetailedState.CONNECTED) { IWifiManager wifiManager = IWifiManager.Stub.asInterface(ServiceManager.getService(Context.WIFI_SERVICE)); Network nw; try { nw = wifiManager.getCurrentNetwork(); } catch (RemoteException e) { nw = null; } NetworkCapabilities nc = cm.getNetworkCapabilities(nw); if (nc != null && !nc.hasCapability(nc.NET_CAPABILITY_VALIDATED)) { return context.getString(R.string.wifi_connected_no_internet); } } String[] formats = context .getResources() .getStringArray((ssid == null) ? R.array.wifi_status : R.array.wifi_status_with_ssid); int index = state.ordinal(); if (index >= formats.length || formats[index].length() == 0) { return ""; } return String.format(formats[index], ssid); }
AccessPoint(Context context, Bundle savedState) { super(context); setWidgetLayoutResource(R.layout.preference_widget_wifi_signal); mConfig = savedState.getParcelable(KEY_CONFIG); if (mConfig != null) { loadConfig(mConfig); } mScanResult = (ScanResult) savedState.getParcelable(KEY_SCANRESULT); if (mScanResult != null) { loadResult(mScanResult); } mInfo = (WifiInfo) savedState.getParcelable(KEY_WIFIINFO); if (savedState.containsKey(KEY_DETAILEDSTATE)) { mState = DetailedState.valueOf(savedState.getString(KEY_DETAILEDSTATE)); } update(mInfo, mState); }