Reputation: 702
I am using Geolocator package for one of my flutter project, and I want to get my current location in periodic cycle of time. To get the permission, I edited AndroidManifest.xml and added those line:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
All things are working just fine, but when I use the apk in one of android 10 phone, everything go south! First of all whenever I am checking the status of permission for the first time of app using
await Geolocator.checkPermission();
It returns .denied
in android 9, and .deniedForever
in android 10, and when to check permission, the android permission seeking popup raised, in android 9 or bellow, the allow button set the permission to the .always
but in android 10, is set to .whileInUse
and I cannot find any option in setting to set it to .always
manually.
After some research, I learn that, I have to add
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
I added them, but still android 10 does not act like android 9, still the "always" option in app manager are missing!
The main problem I am encountering is , whenever I off the screen, the Geolocator.getCurrentPosition()
just stopped working but the same code works perfectly in android 9.
Simulation -
class _MyHomePageState extends State<MyHomePage> {
List _loc = [];
bool _isSwitchon = false;
int _sec = 0;
String _permissonStatus = "";
Future _incrementCounter() async {
Timer.periodic(Duration(seconds: 1), (timer) async {
setState(() {
_sec++;
});
if (_isSwitchon == false) {
timer.cancel();
setState(() {});
} else if (_isSwitchon == true) {
Position currentPosition = await Geolocator.getCurrentPosition();
setState(() {
_loc.add(currentPosition.toString());
});
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Time Elapsed"),
Text(_sec.toString() + " sec"),
Text(
'current Location',
),
Container(
height: 300,
child: ListView.builder(
itemCount: _loc.length,
itemBuilder: (BuildContext context, int i) {
return Text(
_loc[i],
);
},
),
),
Row(
children: [
Expanded(
child: Container(),
),
Column(
children: [
FloatingActionButton(
onPressed: () {
setState(() {
_isSwitchon = true;
});
_incrementCounter();
},
child: Icon(Icons.play_arrow),
),
Text("Start")
],
),
SizedBox(
width: 50,
),
Column(
children: [
FloatingActionButton(
onPressed: () {
setState(() {
_isSwitchon = false;
});
},
child: Icon(Icons.cancel),
),
Text("Stop")
],
),
Expanded(
child: Container(),
)
],
),
Text(_permissonStatus),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
children: [
FloatingActionButton(
onPressed: () async {
await Geolocator.checkPermission().then((value) =>
setState(
() => _permissonStatus = value.toString()));
},
child: Icon(Icons
.signal_wifi_statusbar_connected_no_internet_4_sharp),
),
Text("status")
],
),
Column(
children: [
FloatingActionButton(
onPressed: () {
setState(() {
_loc = [];
});
},
tooltip: 'Increment',
child: Icon(Icons.add),
),
Text("clear Location")
],
),
Column(
children: [
FloatingActionButton(
onPressed: () async {
await Geolocator.requestPermission();
},
tooltip: 'Increment',
child: Icon(Icons.gps_fixed),
),
Text("Permission Seeking")
],
),
],
)
],
),
),
);
}
}
Scenario in my manifest -
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gps_test">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
<application
Upvotes: 0
Views: 4088
Reputation: 3152
You were talking about android 10, I dont see why there is not the right button, but to be future proof this method is outdated anyways.
On Android 11 (API level 30) and higher, however, the system dialog doesn't include the Allow all the time option. Instead, users must enable background location on a settings page, [...].
You should be able to open the permission settings with this package, either via .openLocationSettings()
or .openAppSettings()
and then the user has to navigate to permissions.
Upvotes: 4