答案 0 :(得分:2)
您需要为此创建一个广播寄存器,如果该寄存器检测到wifi状态发生任何变化,就会触发广播意图。
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
创建一个本地广播,收听此更新。 例如
BroadcastReceiver WifiChangeReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action != null && action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
Log.d(AppConstants.TAG, "onReceive: \"wifi connected:\" " + networkInfo.isConnected());
if (networkInfo.isConnected()) {
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if (wifiInfo.getSSID().contains("your desired SSID")/*("Android")*/) {
isAllowed = true;
Log.d(AppConstants.TAG, "==============================onReceive: " + isAllowed);
Log.d(AppConstants.TAG, "onReceive:connected to wifi " + wifiInfo.getSSID());
} else {
Log.d(AppConstants.TAG, "onReceive:connected to wifi " + wifiInfo.getSSID());
isAllowed = false;
}
}
}
}
};
如果您已连接wifi,则表示您已通过正确的凭据 否则是错误的