diff --git a/packages/location/android/src/main/java/com/lyokone/location/MethodCallHandlerImpl.java b/packages/location/android/src/main/java/com/lyokone/location/MethodCallHandlerImpl.java index b2985f68..491ab3b1 100644 --- a/packages/location/android/src/main/java/com/lyokone/location/MethodCallHandlerImpl.java +++ b/packages/location/android/src/main/java/com/lyokone/location/MethodCallHandlerImpl.java @@ -163,8 +163,9 @@ private void isBackgroundModeEnabled(Result result) { private void enableBackgroundMode(MethodCall call, Result result) { final Boolean enable = call.argument("enable"); + final Boolean checkBackgroundPermissions = call.argument("checkBackgroundPermissions"); if (locationService != null && enable != null) { - if (locationService.checkBackgroundPermissions()) { + if (!checkBackgroundPermissions || locationService.checkBackgroundPermissions()) { if (enable) { locationService.enableBackgroundMode(); diff --git a/packages/location/lib/location.dart b/packages/location/lib/location.dart index 92a109ae..43787d52 100644 --- a/packages/location/lib/location.dart +++ b/packages/location/lib/location.dart @@ -47,8 +47,14 @@ class Location implements LocationPlatform { /// Enables or disables service in the background mode. @override - Future enableBackgroundMode({bool? enable = true}) { - return LocationPlatform.instance.enableBackgroundMode(enable: enable); + Future enableBackgroundMode({ + bool? enable = true, + bool? checkBackgroundPermissions = false, + }) { + return LocationPlatform.instance.enableBackgroundMode( + enable: enable, + checkBackgroundPermissions: checkBackgroundPermissions, + ); } /// Gets the current location of the user. diff --git a/packages/location_platform_interface/lib/location_platform_interface.dart b/packages/location_platform_interface/lib/location_platform_interface.dart index 2fff3c90..42b35911 100644 --- a/packages/location_platform_interface/lib/location_platform_interface.dart +++ b/packages/location_platform_interface/lib/location_platform_interface.dart @@ -8,6 +8,7 @@ import 'package:flutter/services.dart'; import 'package:plugin_platform_interface/plugin_platform_interface.dart'; part 'src/method_channel_location.dart'; + part 'src/types.dart'; /// The interface that implementations of `location` must extend. @@ -53,7 +54,10 @@ class LocationPlatform extends PlatformInterface { } /// Enables or disables service in the background mode. - Future enableBackgroundMode({bool? enable}) { + Future enableBackgroundMode({ + bool? enable, + bool? checkBackgroundPermissions, + }) { throw UnimplementedError(); } diff --git a/packages/location_platform_interface/lib/src/method_channel_location.dart b/packages/location_platform_interface/lib/src/method_channel_location.dart index 0aea662b..b9ad6ba3 100644 --- a/packages/location_platform_interface/lib/src/method_channel_location.dart +++ b/packages/location_platform_interface/lib/src/method_channel_location.dart @@ -58,16 +58,22 @@ class MethodChannelLocation extends LocationPlatform { @override Future isBackgroundModeEnabled() async { final result = - await _methodChannel!.invokeMethod('isBackgroundModeEnabled'); + await _methodChannel!.invokeMethod('isBackgroundModeEnabled'); return result == 1; } /// Enables or disables service in the background mode. @override - Future enableBackgroundMode({bool? enable}) async { + Future enableBackgroundMode({ + bool? enable, + bool? checkBackgroundPermissions, + }) async { final result = await _methodChannel!.invokeMethod( 'enableBackgroundMode', - {'enable': enable}, + { + 'enable': enable, + 'checkBackgroundPermissions': checkBackgroundPermissions, + }, ); return result == 1; @@ -80,7 +86,7 @@ class MethodChannelLocation extends LocationPlatform { @override Future getLocation() async { final resultMap = - await _methodChannel!.invokeMapMethod('getLocation'); + await _methodChannel!.invokeMapMethod('getLocation'); if (resultMap == null) { throw PlatformException( code: 'NULL_RESULT',