Reputation: 803
If I need to implement GPS in my application which should be active only if the application is in the foreground - user do something in one activity, change this activity to another etc., but when the user turns off the app or minimizes it then I want it to turn off.
I wonder which method would be better. Implement GPS in Application class or Service?
Upvotes: 1
Views: 277
Reputation: 1226
Implement it in the application class, and then override on the OnResume and OnPause methods to enable and disable the GPS listener
Upvotes: 3
Reputation: 478
I don't know exactly where it is better, but wherever you put the location listener, you cannot exactly know when the Android system kills your application. To know when your application is in foreground/background you can implement an bind service, which is binded from onStart() method of every activity you have in application, and unbind in onStop(). If there are not activities binded, the service stops automatically, and then you remove the location updates listener. I hope this helps you.
Upvotes: 1