I've used this BroadcastReceiver
as GPS status receiver to monitor when user turns on/off his Location in top navigation menu. It suddenly stopped working (whole receiver onReceive()
method is not called) 2 weeks ago (probably has something to do with Android 10 release). Do you know what could be wrong?
It worked perfectly before.
class GPSReceiver: BroadcastReceiver(){
companion object{
const val GPS_PAYLOAD = "gps_payload"
const val GPS_STATE = "gps_state"
}
override fun onReceive(context: Context, intent: Intent) {
App.log("IsGPSEnabled: callingonReceive")
val action = intent.action
if(action != null && action == LocationManager.PROVIDERS_CHANGED_ACTION){
try {
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
val int = Intent(GPS_PAYLOAD)
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
int.putExtra(GPS_STATE, true)
} else {
int.putExtra(GPS_STATE, false)
}
LocalBroadcastManager.getInstance(context).sendBroadcast(int)
} catch (ex: Exception) {
App.log("IsGPSEnabled: $ex")
}
}
}
}
AndroidManifest:
<!-- GPS status receiver -->
<receiver android:name=".services.GPSReceiver"
android:exported="false">
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Android 8.0+ manifest receiver is obsolete
Registering object BroadcastReceiver monitoring intent in Activity:
registerReceiver(gpsStatusReceiver, IntentFilter("android.location.PROVIDERS_CHANGED"))