2

I'm writing a simple distance-tracking app in Unity (because I'm much, much more familiar with it than normal Android development environments and the app is intended for personal use), and my problem is that, when the phone is locked or (presumably) the app closed, data is not collected.

The way the app works is that it queries location every few meters, using Unity's Location Service interface, and adds it up to a total distance once the run is finished.

The app is intended to run on Android 9, and it must measure distance precisely for the duration of at least 1-2 hours. Therefore, I need the battery to last at least that long, especially when Bluetooth services, such as music or heart-rate syncing from a watch, are running alongside it.

My experience with Android development is very low; almost nonexistent. I realize I need some sort of background process, but I'm not sure which, and nor which works best with Unity's built-in Location Service API, or Unity in general.

My research so far tells the process should, in fact, be a foreground process, because newer versions of Android restrict background services to querying for location only once every few hours. Further research says I should work with Work Manager, though I'm still not sure if that's correct. Is measuring location every second or so considered deferrable?

Perhaps I should use an IntentService instead? I'm confused.

What is the appropriate and up-to-date service to use for this purpose?

verified_tinker
  • 625
  • 5
  • 17

1 Answers1

1

The right way to improve the battery life and work in background is to make an Android Service.

You start this services from your unity app, and start collecting data. When you return or reopen your unity app you contact with the services and collect all data from it.

There you have a project to help on Android Services and here a post that might help you too.

About the Android services, you can use bindservice type to bind your unity to the services and autodestroy it when close your app.

And for the limitation of few refresh of location you need to use the google FLP and call LocationRequest.

In LocationRequest you set your update priority with setPriority(int) set to PRIORITY_HIGH_ACCURACY and setInterval(long) for your update interval in secs

joreldraw
  • 1,736
  • 1
  • 14
  • 28
  • When I said I'm no good at Android development, I did mean it, so I hope you don't mind a few questions. 1) Should I write the geolocation code entirely in a Java class, even when the app is open? 2) Where does the Java class *go*, in relation to my Unity project? 3) Is the bound service destroyed when the user closes the app by exiting to the home screen, or by swiping the app away in the "Recents" view, or when Unity calls `Application.Quit()`? – verified_tinker Mar 31 '20 at 12:13
  • Alright, so I've been reading up on Android development, and I think I'm starting to understand. But, as I understand it, I need a separate, Java "add-on" for this to work, right? Can I do this *without* that? If not, then could you give me clear instructions on how I can build that add-on? Because I'm not sure how add-ons even work; is it a separate app or *what*? – verified_tinker Apr 01 '20 at 11:25