Android Wifi 广播接收器未按预期工作
Android Wifi broadcast receiver not working as expected
每当 phone 连接到新的 WiFi 网络时,我都必须获取 WiFi ssid。我注册了以下内容
receiver `registerReceiver(this.mybroadcast, new IntentFilter(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION));`
这部分代码完美运行。但是在onReceive
方法中,
NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO));
始终returns 为空。我也试过 EXTRA_NETWORK_INFO
,但它也是 returns Null。
我不会用
this.registerReceiver(this.mybroadcast, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));,
因为服务将在 n 分钟后 stop/start 并且使用它会在每次重新启动服务时生成事件。
有什么办法可以解决这个问题吗?
您可以在 activity
内创建广播
Step 1 :
@Override
public void onResume() {
registerReceiver();
super.onResume();
}
private void registerReceiver() {
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
registerReceiver(mReceiver, filter);
}
Step 2
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()) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String ssid = wifiInfo.getSSID();
if (ssid != null) {
if (isConnectClicked) {
//YOUR CONNECTED WITH WIFI
}
}
}
}
} else {
if (intent.getAction().equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {
if (intent.hasExtra(WifiManager.EXTRA_SUPPLICANT_ERROR)) {
//Connection failed
}
}
}
}
};
Step 3
@Override
public void onPause() {
unregisterReceiver(mReceiver);
super.onPause();
}
每当 phone 连接到新的 WiFi 网络时,我都必须获取 WiFi ssid。我注册了以下内容
receiver `registerReceiver(this.mybroadcast, new IntentFilter(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION));`
这部分代码完美运行。但是在onReceive
方法中,
NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO));
始终returns 为空。我也试过 EXTRA_NETWORK_INFO
,但它也是 returns Null。
我不会用
this.registerReceiver(this.mybroadcast, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));,
因为服务将在 n 分钟后 stop/start 并且使用它会在每次重新启动服务时生成事件。
有什么办法可以解决这个问题吗?
您可以在 activity
内创建广播Step 1 :
@Override
public void onResume() {
registerReceiver();
super.onResume();
}
private void registerReceiver() {
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
registerReceiver(mReceiver, filter);
}
Step 2
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()) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String ssid = wifiInfo.getSSID();
if (ssid != null) {
if (isConnectClicked) {
//YOUR CONNECTED WITH WIFI
}
}
}
}
} else {
if (intent.getAction().equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {
if (intent.hasExtra(WifiManager.EXTRA_SUPPLICANT_ERROR)) {
//Connection failed
}
}
}
}
};
Step 3
@Override
public void onPause() {
unregisterReceiver(mReceiver);
super.onPause();
}