Reputation: 311
I have 2 scripts where i try to enable GPS but none of them seem to work , so how i can enable GPS for a foreground - background task for some minutes and then close it again ? (not manually but programmatically through script) .
(if of course user give me full permision to run location on background)
Script 1 :
Future<void> setupBackgroundLocation() async {
// Set up background location
await BackgroundLocation.setAndroidNotification(
title: "Notification title",
message: "Notification message",
icon: "@mipmap/ic_launcher",
);
await BackgroundLocation.setAndroidConfiguration(1000);
// Start the location service
BackgroundLocation.startLocationService(
forceAndroidLocationManager: true, distanceFilter: 0);
BackgroundLocation.getLocationUpdates((location) {
try {
print(location);
} catch (e) {
print("Error getting location updates: $e");
}
});
BackgroundLocation.stopLocationService();
}
Script 2 :
import 'dart:async';
import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:location/location.dart';
@pragma('vm:entry-point')
Future<void> onStart(ServiceInstance service) async {
Timer.periodic(Duration(seconds: 20), (Timer timer) async {
try {
Location location = Location();
bool _serviceEnabled;
PermissionStatus _permissionGranted;
LocationData _locationData;
_serviceEnabled = await location.serviceEnabled();
if (!_serviceEnabled) {
_serviceEnabled = await location.requestService();
if (!_serviceEnabled) {
return;
}
}
_permissionGranted = await location.hasPermission();
if (_permissionGranted == PermissionStatus.denied) {
_permissionGranted = await location.requestPermission();
if (_permissionGranted != PermissionStatus.granted) {
return;
}
}
_locationData = await location.getLocation();
location.onLocationChanged.listen((LocationData currentLocation) {});
location.enableBackgroundMode(enable: true);
} catch (e) {
print('Error $e');
}
});
}
Ive also tried Geolocator Package but i cant find any command to enable it, here is the script also if that helps :
import 'dart:async';
import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:geolocator/geolocator.dart';
@pragma('vm:entry-point')
Future<void> onStart(ServiceInstance service) async {
StreamSubscription<Position>? positionStreamSubscription;
late LocationSettings locationSettings;
locationSettings = AndroidSettings(
accuracy: LocationAccuracy.high,
forceLocationManager: false,
intervalDuration: const Duration(milliseconds: 500),
//distanceFilter: 50,
);
Geolocator.getPositionStream(locationSettings: locationSettings);
Timer.periodic(Duration(seconds: 20), (Timer timer) async {
positionStreamSubscription?.cancel();
try {
positionStreamSubscription = Geolocator.getPositionStream(
locationSettings:
AndroidSettings(intervalDuration: Duration(seconds: 1)))
.listen((Position position) async {
double geofenceLatitude = xxxxx;
double geofenceLongitude = xxxxx;
double geofenceRadius = 60;
double distance = await Geolocator.distanceBetween(
position.latitude,
position.longitude,
geofenceLatitude,
geofenceLongitude,
);
if (distance <= geofenceRadius) {
print('Inside geofence 100');
// positionStreamSubscription?.cancel();
// await service.stopSelf();
} else {
print('Outside geofence 100');
}
});
} catch (e) {
print(e);
}
});
}
So do i need some native code or from Android 10-14 and higher we must only take the user permission from a pop up window ??
Thanks .
Upvotes: 0
Views: 190
Reputation: 93688
You can't, for security reasons. Only the user can enable or disable GPS, so the user can protect his privacy from apps. All you can do is check if it's enabled, and ask the user to turn it on himself.
Upvotes: 0