與WifiManager.SUPPLICANT_STATE_CHANGED_ACTION
註冊接收器收到通知時,連接失敗occur.it可能是由於用於連接到WiFi
提供憑據無效
private void registerReceiver() {
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
registerReceiver(mReceiver, filter);
}
和接收器下方
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if (info != null) {
if (info.isConnected()) {
//connected
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String ssid = wifiInfo.getSSID();
}
} else {
if (intent.getAction().equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {
if (intent.hasExtra(WifiManager.EXTRA_SUPPLICANT_ERROR)) {
//failed to connect
}
}
}
};
比k你和我很抱歉遲到的回覆。 – user3752013