question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

watchLocation method doesn't get location in background mode android

See original GitHub issue

Which platform(s) does your issue occur on? Android Android 10 Device Moto G(2nd Generation)

Please, provide the following version numbers that your issue occurs with: CLI: 8.3.18

In, package.json “nativescript-geolocation”: “^5.1.0”,

Please, tell us how to recreate the issue in as much detail as possible.

I’m using Geolocation apis to get location in background in android. When I call watchLocation in foreground, it’s giving location, but when app goes to background, I didn’t get any location. I am displaying location in console.log. For ios, when app goes in background mode, it’s display log of location. I am using nativescript with angular.

Is there any code involved?

public buttonStartTap() {
   console.log("buttonStartTap");
   try {
     let that = this;
     this.watchIds.push(geolocation.watchLocation(
       function (loc) {
         if (loc) {
           that.locations.push(loc);
           console.log("loc", loc);
         }
       },
       function (e) {
         console.log("Error: " + e.message);
       },
       {
         desiredAccuracy: Accuracy.high,
         updateDistance: 1,
         updateTime: 3000,
         minimumUpdateTime: 100,
         iosAllowsBackgroundLocationUpdates: true,
         iosPausesLocationUpdatesAutomatically: false,

       }));
   } catch (ex) {
     console.log("Error: " + ex.message);
   }
 }

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:15

github_iconTop GitHub Comments

1reaction
butaminascommented, Apr 24, 2020

I think I found the solution!!! Hopefully, more can test this and confirm.

What I did was I added android:foregroundServiceType="location" to my ForegroundService Service in Android.xml

My final service tag looks like this:

        <service
                android:name="your-name-here"
                android:foregroundServiceType="location"
                android:stopWithTask="true"
                android:enabled="true"
                android:exported="false" />

My minSdkVersion is 21

Hope it helps someone as this was really painful to me… 🤯

1reaction
akarsh014commented, Apr 1, 2020

I still the same issue, i get only few coordinates after which “service onStopJob” is called

Read more comments on GitHub >

github_iconTop Results From Across the Web

It is possible to watch the location in the background on ...
I have checked it on Chrome (Android M), and as soon as the Web App goes to background the GPS icon disappears and...
Read more >
Access location in the background - Android Developers
In your app's manifest, check for the ACCESS_COARSE_LOCATION permission and the ACCESS_FINE_LOCATION permission. Verify that your app requires ...
Read more >
Geolocation disabled when Chrome is in the background or ...
It would make sense to reconsider this change and implement watchposition back into Chromium for use in the background. It seems backwards to...
Read more >
BackgroundGeolocation | React Native Background Geolocation
ℹ️ You can manually request the current-state of "Power Saving" mode with the method isPowerSaveMode. iOS. iOS Power Saving mode can be engaged...
Read more >
Location - Expo Documentation
It's also discouraged to use geocoding while the app is in the background and its results won't be shown to the user immediately....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found