Example app changes and android implementation finished
ADD
geolocator/android/src/main/java/com/baseflow/geolocator/LocationServiceHandlerImpl.java
+package com.baseflow.geolocator;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.location.LocationManager;
+import android.util.Log;
+
+import androidx.annotation.Nullable;
+
+import com.baseflow.geolocator.location.LocationServiceStatusReceiver;
+
+import io.flutter.plugin.common.BinaryMessenger;
+import io.flutter.plugin.common.EventChannel;
+
+public class LocationServiceHandlerImpl implements EventChannel.StreamHandler{
+
+ private static final String TAG = "LocationServiceHandler";
+
+ @Nullable private EventChannel channel;
+ @Nullable private Activity activity;
+ @Nullable private Context context;
+ @Nullable private LocationServiceStatusReceiver receiver;
+
+
+ void startListening(Context context, BinaryMessenger messenger){
+ if(channel != null){
+ Log.w(TAG, "Setting a event call handler before the last was disposed.");
+ stopListening();
+ }
+ channel = new EventChannel(messenger, "flutter.baseflow.com/geolocator_service_updates");
+ channel.setStreamHandler(this);
+ this.context = context;
+ }
+
+ void stopListening(){
+ if(channel == null){
+ return;
+ }
+ channel.setStreamHandler(null);
+ channel = null;
+ }
+
+ void setActivity(@Nullable Activity activity) { this.activity = activity;}
+
+ @Override
+ public void onListen(Object arguments, EventChannel.EventSink events) {
+ IntentFilter filter = new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION);
+ filter.addAction(Intent.ACTION_PROVIDER_CHANGED);
+ this.receiver = new LocationServiceStatusReceiver(arguments, events);
+ if(activity == null) return;
+ activity.registerReceiver(this.receiver, filter);
+ }
+
+ @Override
+ public void onCancel(Object arguments) {
+ activity.unregisterReceiver(this.receiver);
+ }
+
+}

ADD
geolocator/android/src/main/java/com/baseflow/geolocator/location/LocationServiceStatusReceiver.java
+package com.baseflow.geolocator.location;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.location.LocationManager;
+
+import androidx.annotation.Nullable;
+
+import io.flutter.plugin.common.EventChannel;
+
+
+public class LocationServiceStatusReceiver extends BroadcastReceiver {
+ @Nullable
+ private final EventChannel.EventSink events;
+
+ private boolean isEnabled = false;
+
+ public LocationServiceStatusReceiver(@Nullable Object arguments, @Nullable EventChannel.EventSink events) {
+ this.events = events;
+ }
+
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ if (LocationManager.PROVIDERS_CHANGED_ACTION.equals(intent.getAction())) {
+
+ LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
+ boolean isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
+ boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
+
+
+ if (isGpsEnabled) {
+ /*
+ It may occur that the broadcastreceiver receives several events of the same type,
+ that's why u should check whether or not the event is fired more then one time.
+ This is realised by using the [isEnabled] boolean
+ */
+ if(!isEnabled){
+ isEnabled = true;
+ if (events != null) {
+ events.success(true);
+ }
+ }
+ } else {
+ if(isEnabled){
+ isEnabled = false;
+ if (events != null) {
+ events.success(false);
+ }
+ }
+
+ }
+ }
+ }
+}

MODIFY
geolocator/example/lib/main.dart
geolocator/example/lib/main.dart
@@ -32,6 +32,7 @@ class GeolocatorWidget extends StatefulWidget {
class _GeolocatorWidgetState extends State<GeolocatorWidget> {
final List<_PositionItem> _positionItems = <_PositionItem>[];
StreamSubscription<Position>? _positionStreamSubscription;
+ StreamSubscription<bool>? _locationServiceStatusSubscription;
@override
Widget build(BuildContext context) {
@@ -114,7 +115,7 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
onPressed: _toggleListening,
label: Text(() {
if (_positionStreamSubscription == null) {
- return "Start stream";
+ return "Start Position Updates stream";
} else {
final buttonText = _positionStreamSubscription!.isPaused
? "Resume"
@@ -126,6 +127,23 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
backgroundColor: _determineButtonColor(),
),
),
+ Positioned(
+ bottom: 430.0,
+ right: 10.0,
+ child: FloatingActionButton.extended(
+ onPressed: _toggleLocationServiceListener,
+ label: Text(() {
+ if(_locationServiceStatusSubscription == null) {
+ return "Start Location Service stream";
+ } else {
+ final buttonText = _locationServiceStatusSubscription!.isPaused
+ ? "Resume"
+ : "Pause";
+ return "$buttonText stream";
+ }
+ }())
+ )
+ ),
Positioned(
bottom: 290.0,
right: 10.0,
@@ -166,6 +184,29 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
return _isListening() ? Colors.green : Colors.red;
}
+ void _toggleLocationServiceListener() {
+ if(_locationServiceStatusSubscription == null){
+ final serviceStatusStream = Geolocator.getServiceStatusStream();
+ _locationServiceStatusSubscription = serviceStatusStream.handleError((error){
+ _locationServiceStatusSubscription?.cancel();
+ _locationServiceStatusSubscription = null;
+ }).listen((status) => setState(() => _positionItems.add(_PositionItem(
+ _PositionItemType.locationservicestatus, status.toString()))));
+ _locationServiceStatusSubscription?.pause();
+ }
+
+ setState((){
+ if(_locationServiceStatusSubscription == null){
+ return;
+ }
+ if(_locationServiceStatusSubscription!.isPaused) {
+ _locationServiceStatusSubscription!.resume();
+ }else{
+ _locationServiceStatusSubscription!.pause();
+ }
+ });
+ }
+
void _toggleListening() {
if (_positionStreamSubscription == null) {
final positionStream = Geolocator.getPositionStream();
@@ -204,6 +245,7 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
enum _PositionItemType {
permission,
position,
+ locationservicestatus
}
class _PositionItem {

Example app changes and android implementation finished