BroadcastReceiver doesn't work as expected. Its onReceive() method is not called.
Sample code:
public class MainActivity extends AppCompatActivity {
private WifiManager wifiManager;
private final WifiStateReceiver wifiStateReceiver = new WifiStateReceiver();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter intentFilter = new IntentFilter(WifiManager.RSSI_CHANGED_ACTION);
registerReceiver(wifiStateReceiver, intentFilter);
wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.startScan();
}
@Override
protected void onDestroy() {
unregisterReceiver(wifiStateReceiver);
super.onDestroy();
}
private class WifiStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.v("WifiStateReceiver", "WifiStateReceiver.onReceive() is called. Action: " + intent.getAction());
}
}
}
Permissions:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
All permissions are granted. I tested this code on Android 8 and Android 10.
from Broadcast receiver with RSSI_CHANGED_ACTION never called
No comments:
Post a Comment