1

I made an Android app that makes lots of stuff. One of them is GPS tracking, saving marks to draw a trail. Sometimes, Android closes my app when the phone is out of my sight in my pocket (I guess it's Android's "smart" memory management...??), and thus its GPS tracking stops.

I have used some tracking apps that keep running for long periods, so I guess they do something that I don't know.

How can I prevent this sudden stop? Can I add some kind of priority to the GPS tracking to prevent this, or some other way?

EDIT

Observe that I don't want just to get tracking even if app is closed. I want to prevent Android from closing the app, since the GPS tracking needs to be logged by the app.

Luis A. Florit
  • 2,169
  • 1
  • 33
  • 58
  • Possible duplicate of [GPS Location tracking even when app is closed(Not running in Background)/ScreenLocked](https://stackoverflow.com/questions/41023986/gps-location-tracking-even-when-app-is-closednot-running-in-background-screenl) – P.Juni Jul 21 '19 at 00:20
  • Not really. I don't want to get tracking even if app is closed. I want to prevent Android from closing the app, since the GPS tracking needs to be logged by the app. – Luis A. Florit Jul 21 '19 at 04:16
  • How about service with `START_STICKY` option ? – P.Juni Jul 21 '19 at 09:59
  • This post https://stackoverflow.com/q/42126979/5909412 – P.Juni Jul 21 '19 at 10:00
  • Maybe your `START_STICKY` suggestion works... (maybe I can even add a `wakeLock`?) I guess I only need to add this code to the service class (not sure how to check if this is working): `@Override public int onStartCommand(Intent intent, int flags, int startId) { return START_STICKY; }` – Luis A. Florit Jul 21 '19 at 14:34
  • I'm not sure if I can add a `wakeLock` to a service...? – Luis A. Florit Jul 21 '19 at 14:46
  • You have to serach a bit https://developer.android.com/training/scheduling/wakelock :) – P.Juni Jul 21 '19 at 14:51
  • Ok, I went for `wakeLock` + `START_STICKY`. I will report later how this worked. Thanks!! – Luis A. Florit Jul 21 '19 at 21:03
  • If it works, we can post it as a valid answer – P.Juni Jul 22 '19 at 03:41
  • Maybe you can add it now, since it will take a while until I can fully check this. – Luis A. Florit Jul 22 '19 at 03:58

2 Answers2

1

I think what you are looking for is to receive location updates even if after closing app, This tutorial will help.

Khalid Taha
  • 3,183
  • 5
  • 27
  • 43
  • Not really. I need the GPS data to be logged by the app, so I don't want the app to be closed. And my app needs to work for Android >= 2.3.6, not just "O". – Luis A. Florit Jul 21 '19 at 04:19
  • @LuisA.Florit this code works for each android version, and also works when app is open and closed, you can turn it off also when closing app, it has every scenario that you want. – Khalid Taha Jul 21 '19 at 08:31
  • @LuisA.Florit if you want your app not to be closed at all, then this is not development option, this is OS option that is not allowed for you. – Khalid Taha Jul 21 '19 at 08:32
0

Create ForegroundService. System will not drop it until you close it by yourself. More about in Documentation

Marat Zangiev
  • 1,172
  • 7
  • 12
  • My GPS service is actually a bound service, since I want the app to be able to start/stop the service, and that the user is aware that the app has started the service and can stop it. – Luis A. Florit Jul 21 '19 at 13:06