public void onReceive(Context paramContext, Intent paramIntent) { if (paramIntent.getAction().equals( // 搜索到wifi热点 WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) { Log.d(TAG, WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); for (int j = 0; j < ehList.size(); j++) ((EventHandler) ehList.get(j)).scanResultsAvailable(); } else if (paramIntent.getAction().equals( // wifi打开或关闭 WifiManager.WIFI_STATE_CHANGED_ACTION)) { Log.d(TAG, WifiManager.WIFI_STATE_CHANGED_ACTION + " | " + paramIntent.getIntExtra("wifi_state", -1) + " | " + paramIntent.getIntExtra("previous_wifi_state", -1)); for (int j = 0; j < ehList.size(); j++) ((EventHandler) ehList.get(j)).wifiStatusNotification(); } else if (paramIntent.getAction().equals( // 连接 SSID WifiManager.NETWORK_STATE_CHANGED_ACTION)) { mNetworkInfo = paramIntent.getParcelableExtra("networkInfo"); Log.d(TAG, WifiManager.NETWORK_STATE_CHANGED_ACTION + " | " + mNetworkInfo.getDetailedState()); // 当 DetailedState 变化为 CONNECTED 时,说明已连接成功,则通知Handler更新 // 可避免WifiapActivity里出现重复获取IP的问题 if (mNetworkInfo.getDetailedState() == NetworkInfo.DetailedState.CONNECTED) { for (int i = 0; i < ehList.size(); i++) ((EventHandler) ehList.get(i)).handleConnectChange(); } } }
@Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = cm.getActiveNetworkInfo(); if (networkInfo == null || networkInfo.getDetailedState() == NetworkInfo.DetailedState.DISCONNECTED) { // Wifi is disconnected Log.i(TAG, "Network is disconnected: " + networkInfo); } else if (networkInfo.getDetailedState() == NetworkInfo.DetailedState.CONNECTED) { Log.i( TAG, "Network is connected: " + networkInfo + " type = " + networkInfo.getTypeName() + " : " + networkInfo.getSubtypeName()); // Intent i = new Intent(context, RviService.class); // i.putExtra("networkinfo", networkInfo); // context.startService(i); } } }
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; }
@Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) { final NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); final NetworkInfo.DetailedState detailed = networkInfo.getDetailedState(); if (detailed != NetworkInfo.DetailedState.DISCONNECTED && detailed != NetworkInfo.DetailedState.DISCONNECTING && detailed != NetworkInfo.DetailedState.SCANNING) { if (!mReenabled) { mReenabled = true; reenableAllAps(context); stopSelf(); } } } }
private void handleEvent(Context context, Intent intent) { String action = intent.getAction(); if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) { updateWifiState( intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN)); } else if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action) || WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION.equals(action) || WifiManager.LINK_CONFIGURATION_CHANGED_ACTION.equals(action)) { updateAccessPoints(); } else if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(action)) { // Ignore supplicant state changes when network is connected // TODO: we should deprecate SUPPLICANT_STATE_CHANGED_ACTION and // introduce a broadcast that combines the supplicant and network // network state change events so the apps dont have to worry about // ignoring supplicant state change when network is connected // to get more fine grained information. SupplicantState state = (SupplicantState) intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE); if (!mConnected.get() && SupplicantState.isHandshakeState(state)) { updateConnectionState(WifiInfo.getDetailedStateOf(state)); } else { // During a connect, we may have the supplicant // state change affect the detailed network state. // Make sure a lost connection is updated as well. updateConnectionState(null); } } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) { NetworkInfo info = (NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); mConnected.set(info.isConnected()); changeNextButtonState(info.isConnected()); updateAccessPoints(); updateConnectionState(info.getDetailedState()); if (mAutoFinishOnConnection && info.isConnected()) { Activity activity = getActivity(); if (activity != null) { activity.setResult(Activity.RESULT_OK); activity.finish(); } return; } } else if (WifiManager.RSSI_CHANGED_ACTION.equals(action)) { updateConnectionState(null); } }
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; }
/** * 1) Идет проверка на состояние сети. Если произошло подключение к сети, но сеть имеет неверное * название, то отсылается броадкаст на переподключение * * <p>2) Проверяется состояние wi-fi модуля. Ксли модуль подключен - отсылаем броадкаст на бинд * сервиса Ксли модуль отключен - отсылаем броадкаст на анбинд * * @param context * @param intent */ @Override public void onReceive(Context context, Intent intent) { WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); NetworkInfo.DetailedState state = info.getDetailedState(); if (state == NetworkInfo.DetailedState.CONNECTED) { if (!mWifiManager.getConnectionInfo().getSSID().equals(wifiName) && !mWifiManager.getConnectionInfo().getSSID().equals("\"" + wifiName + "\"")) { try { if (mBoundService != null) { mBoundService.reconnectToWifi(); } } catch (Exception e) { e.printStackTrace(); Toast.makeText(activity, "cm_br_reconnect error", Toast.LENGTH_LONG).show(); } } } } else if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) { int status = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN); if (status == WifiManager.WIFI_STATE_ENABLED) { doBindService(); } else if (status == WifiManager.WIFI_STATE_DISABLED) { doUnbindService(); } } else if (intent.getAction().equals(ConnectionManager.WIFI_CONNECTED) || intent.getAction().equals(ConnectionManager.USB_CONNECTED)) { isConnected = true; } else if (intent.getAction().equals(ConnectionManager.WIFI_DISCONNECTED) || intent.getAction().equals(ConnectionManager.USB_DISCONNECTED)) { isConnected = false; } else if (intent.getAction().equals(ConnectionManager.MESSAGE_SEND)) { // TODO: отправка сообщения } } // onReceive
public void onReceive(Context paramContext, Intent paramIntent) { // wifi开关 if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(paramIntent.getAction())) { int wifiState = paramIntent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0); switch (wifiState) { case WifiManager.WIFI_STATE_DISABLED: case WifiManager.WIFI_STATE_ENABLED: mListener.wifiStatusChange(); break; } } // 连接wifi else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(paramIntent.getAction())) { mNetworkInfo = paramIntent.getParcelableExtra("networkInfo"); /** 当 DetailedState 变化为 CONNECTED 时,说明已连接成功,则通知Handler更新 可避免WifiapActivity里出现重复获取IP的问题 */ if (mNetworkInfo.getDetailedState() == NetworkInfo.DetailedState.CONNECTED) { mListener.WifiConnected(); } } }
public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (DBG) log("Receive intent action = " + action); if (action.equals(BluetoothDunService.ACCESS_RESPONSE_ACTION)) { int result = intent.getIntExtra( BluetoothDunService.EXTRA_ACCESS_RESULT, BluetoothDunService.RESULT_USER_REJECT); if (result == BluetoothDunService.RESULT_USER_ACCEPT) { dunConnectRspNative(mDunConnPath, true); } else { dunConnectRspNative(mDunConnPath, false); // dunSetState(BluetoothDun.STATE_DISCONNECTED); } } else if (action.equals(BluetoothDunService.RESEND_NOTIFICATION_ACTION)) { BluetoothDevice device = mAdapter.getRemoteDevice(mDunConnPath); createDunAuthNotification(context, device, true); } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { ConnectivityManager connmgr; connmgr = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo MobileInfo = connmgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); NetworkInfo WifiInfo = connmgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); NetworkInfo.DetailedState MobileState = NetworkInfo.DetailedState.IDLE; NetworkInfo.DetailedState WifiState = NetworkInfo.DetailedState.IDLE; if (MobileInfo != null) MobileState = MobileInfo.getDetailedState(); if (WifiInfo != null) WifiState = WifiInfo.getDetailedState(); if (DBG) log("NetworkInfo broadcast, MobileState=" + MobileState + ",WifiState=" + WifiState); /* if get network service via wifi, get interface name by "wifi.interface" system property key * String interfaceName = SystemProperties.get("wifi.interface", "tiwlan0"); * log("wifi interface name=" + interfaceName); */ if (MobileState == NetworkInfo.DetailedState.IDLE && WifiState == NetworkInfo.DetailedState.IDLE) { if (mDunState == BluetoothDun.STATE_CONNECTED) { dunDisconnectNative(); } } } else if (action.equals(BluetoothTethering.BLUETOOTH_INTERFACE_ADDED)) { if (DBG) log("receiver BluetoothTethering.BLUETOOTH_INTERFACE_ADDED"); if (mDunState == BluetoothDun.STATE_CONNECTED) { String iface = intent.getStringExtra(BluetoothTethering.BLUETOOTH_INTERFACE_NAME); ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); if (cm.tether(iface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) { Log.e(TAG, "Error tethering " + iface); } } else { if (DBG) log("DUN does not connected"); } } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR) == BluetoothAdapter.STATE_TURNING_OFF) { clearService(); } } }
@Test public void getDetailedState_shouldReturnTheAssignedState() throws Exception { NetworkInfo networkInfo = Robolectric.newInstanceOf(NetworkInfo.class); shadowOf(networkInfo).setDetailedState(NetworkInfo.DetailedState.SCANNING); assertThat(networkInfo.getDetailedState(), equalTo(NetworkInfo.DetailedState.SCANNING)); }
public DetailedState getDetailedState() { return mNetworkInfo != null ? mNetworkInfo.getDetailedState() : null; }