Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: In Android 11 not able to fetch long in background #1588

Open
3 of 8 tasks
Dhiren198 opened this issue Oct 10, 2024 · 2 comments
Open
3 of 8 tasks

[Bug]: In Android 11 not able to fetch long in background #1588

Dhiren198 opened this issue Oct 10, 2024 · 2 comments

Comments

@Dhiren198
Copy link

Please check the following before submitting a new issue.

Please select affected platform(s)

  • Android
  • iOS
  • Linux
  • macOS
  • Web
  • Windows

Steps to reproduce

In Android 11 not able to fetch long in background

Expected results

to get lat long when app in background

Actual results

in background not response and no log producing

Code sample

@pragma('vm:entry-point')
Future<Position?> _determieOffline() async {

late LocationSettings locationSettings;
locationSettings = AndroidSettings(
accuracy: LocationAccuracy.high,
distanceFilter: 100,
forceLocationManager: true,
intervalDuration: const Duration(seconds: 10),

  foregroundNotificationConfig: const ForegroundNotificationConfig(
    notificationText:
        "Example app will continue to receive your location even when you aren't using it",
    notificationTitle: "Running in Background",
    enableWakeLock: true,
  ));

print("before get current position");
Position? position;
try {
position =
await Geolocator.getCurrentPosition(locationSettings: locationSettings);
} catch (e) {
print("the error geooffline is $e");
}
return position;
}

Screenshots or video

Screenshots or video demonstration

[Upload media here]

Version

geolocator: ^13.0.1

Flutter Doctor output

lb@LBs-MacBook-Pro ~ % flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.24.3, on macOS 15.0 24A335 darwin-x64, locale en-IN)
[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 16.0)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.1)
[✓] VS Code (version 1.94.1)
[✓] Connected device (4 available)
[✓] Network resources

• No issues found!
lb@LBs-MacBook-Pro ~ %

@slaci
Copy link

slaci commented Dec 3, 2024

I'm experiencing the same. If I provide a timeLimit then getCurrentPosition will throw TimeoutException after that amount of time, but I don't know how many minutes should be that timeout for anything to happen. There are background location fetch limitations on android:

If your app is running in the background, the location system service computes a new location for your app only a few times each hour. This is the case even when your app is requesting more frequent location updates.

I don't know if it really means your call would wait for minutes to receive a position really.

I can fall back to getLastKnownPosition on timeout, but I have never seen a non-timeout case happening nor on a emulator or real device:

Future<Position?> _getPosition({
    required LocationSettings settings,
    required bool fallbackToLastKnownLocation,
  }) async {
    try {
      return await Geolocator.getCurrentPosition(locationSettings: settings);
    } on TimeoutException {
      if (fallbackToLastKnownLocation) {
        return Geolocator.getLastKnownPosition(
          forceAndroidLocationManager: settings is AndroidSettings
              ? settings.forceLocationManager
              : false,
        );
      }
      rethrow;
    }
  }

However setting the forceLocationManager option to true instantly returns a position, though I could not verify what is that doing. It is called "legacy" stuff in the comments, so not sure if its a good solution.

@slaci
Copy link

slaci commented Dec 5, 2024

After some testings I have some new observations:

  1. If I give the request a high enough timeout (like 1 minute) and open a different app which fetches the current location (like Google Maps), then my app immediately receives that location in the background. So basically only the passive listener client works, but doesn't really requests a location by itself while in the background (or maybe sometimes extremely rarely).

  2. This plugin does not support it, but there is a setGranularity method on the native LocationRequest class. If I call it with GRANULARITY_COARSE then it gives me a position in the background, but only a coarse location of course. So as I see it hangs only when precise location is requested in the background. As the GRANULARITY_PERMISSION_LEVEL granularity is the default (this plugin only uses this) it will always request precise location if the user granted precise location permission, but it won't hang when the user only grants coarse location permission. This option could be added to this plugin.

  3. Just to repeat: When setting forceLocationManager to true then it works instantly most of the time (in the background) without any tricks. I haven't tested it too intensively though.

PS: Starting a foreground service would solve the issue, but I think we are talking about one-off request cases here when that is not feasible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants