onProviderChange static method
Subscribe to changes in device's location-services configuration / authorization.
Your callback
fill be executed whenever a change in the state of the device's Location Services has been detected. eg: "GPS ON", "Wifi only".
NOTE: The plugin always force-fires an onProviderChange
event whenever the app is launched (right after the ready method is executed), regardless of current state, so you can learn the the current state of location-services with each boot of your application.
ProviderChangeEvent.status:
Name | Platform |
---|---|
ProviderChangeEvent.AUTHORIZATION_STATUS_NOT_DETERMINED | iOS only |
ProviderChangeEvent.AUTHORIZATION_STATUS_RESTRICTED | iOS only |
ProviderChangeEvent.AUTHORIZATION_STATUS_DENIED | iOS & Android |
ProviderChangeEvent.AUTHORIZATION_STATUS_ALWAYS | iOS & Android |
ProviderChangeEvent.AUTHORIZATION_STATUS_WHEN_IN_USE | iOS only |
NOTE: When Android location permission is granted,
ProviderChangeEvent.status == ProviderChangeEvent.AUTHORIZATION_STATUS_ALWAYS,
otherwise, ProviderChangeEvent.AUTHORIZATION_STATUS_DENIED.
Example
BackgroundGeolocation.onProviderChange((ProviderChangeEvent event) {
print('[onProviderChange: ${event}');
switch(provider.status) {
case ProviderChangeEvent.AUTHORIZATION_STATUS_DENIED:
// Android & iOS
print('- Location authorization denied');
break;
case ProviderChangeEvent.AUTHORIZATION_STATUS_ALWAYS:
// Android & iOS
console.log('- Location always granted');
break;
case ProviderChangeEvent.AUTHORIZATION_STATUS_WHEN_IN_USE:
// iOS only
console.log('- Location WhenInUse granted');
break;
}
});
Implementation
static void onProviderChange(Function(ProviderChangeEvent) callback) {
if (_eventsProviderChange == null) {
_eventsProviderChange = _eventChannelProviderChange
.receiveBroadcastStream()
.map((dynamic event) {
return ProviderChangeEvent(event);
});
}
_registerSubscription(_eventsProviderChange!.listen(callback), callback);
}