Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] Fetch location from android in background in flutter

Posted on November 11, 2022 By

Solution 1 :

You are probably being hampered by the restrictions brought in with Android 8 (API 26) which limits the frequency that background apps can retrieve the current location.

This was brought in to save battery but it means that you will need to have a visible notification in order for Android to consider the app as being in the foreground. Otherwise it will only retrieve the location a few times per hour whilst the app is in the background.

https://developer.android.com/about/versions/oreo/background-location-limits gives you some further background (excuse the pun) information.

Problem :

I am using below code to get the track the location of user.
This works proper when my application in foreground. But when my application move to background it stop working and i can not get any location.

import 'dart:async';
import 'package:permission_handler/permission_handler.dart';
import 'package:geolocator/geolocator.dart';

class FetchLocation   {
  var geolocator = Geolocator();
  var locationOptions = LocationOptions(accuracy: LocationAccuracy.high, distanceFilter: 10,forceAndroidLocationManager: true,timeInterval: 1);
  void trackGeoLocation()async{
    final PermissionStatus permission = await PermissionHandler()
        .checkPermissionStatus(PermissionGroup.location);
      if(permission == PermissionStatus.granted){
        fetchLocation();
      }else{
        askPermission();
      }

  }
  void askPermission() {
    PermissionHandler().requestPermissions([PermissionGroup.locationAlways]).then(__onStatusRequested);
  }
  void __onStatusRequested(Map<PermissionGroup, PermissionStatus> statuses){
    final status = statuses[PermissionGroup.locationWhenInUse];

    print(status);
    if(status == PermissionStatus.restricted || status == PermissionStatus.neverAskAgain){
    } else if(status == PermissionStatus.denied){
      askPermission();
    }else{
      fetchLocation();
    }
  }
  void fetchLocation(){
    StreamSubscription<Position> positionStream = geolocator.getPositionStream(locationOptions).listen(
            (Position position) {
          print(position == null ? 'Unknown' : position.latitude.toString() + ', ' + position.longitude.toString());
        });
  }
}

Comments

Comment posted by flutter.dev/docs/development/packages-and-plugins/…

Did you read:

Comment posted by pub.dev/packages/background_location

this package might help you check this out

Comment posted by ios developer

SimonH thank you for the answer. Yes it is getting called in background but it get called once in a while after 10-15min. I want to track the location continuously like Uber app.

READ  [FIXED] android - Mock DateFormat in Junit and set Timezone
Powered by Inline Related Posts

Comment posted by Simon Hutton

You’ll need to do something to make the OS think the app is in the foreground (like a visible notification) or run it in a service. I’m sure Google is your friend here. (I removed the link I placed here as it’s a little out of date)

Android Tags:android, android-background, flutter, geolocation

Post navigation

Previous Post: [FIXED] android – Convert BMP image to PNG or JPG in Java without loading the entire image in memory
Next Post: [FIXED] xml – Android – prevent TextView to push out another element inside LinearLayout

Related Posts

[FIXED] java – How can I remove default seekbar stroke? Android
[FIXED] java – which IP address should I make in the Post method? Android
[FIXED] android – can’t get text input from TextInputEditText Material library Android
[FIXED] android – Mockk Mocking Private Properties in Kotlin Android
[FIXED] Error when i use layout tag in databinding in xml android project Android
[FIXED] android – Will creating a Toast inside a Runnable cause a memory leak? Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What are the main features of Islamic education?
  • Is the Jeep 4xe worth it?
  • How does the ringer work on cast iron?
  • What is the biggest size interior door?
  • Is blue raspberry an original Jolly Rancher flavor?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme