2

I'm in the middle of building an Android version of my iOS app and could use some guidance as to how to implement certain features.

The app has several responsibilities; monitoring location changes and adding them to a DB, advertising and discovering available peers via Bonjour, maintaining connections to discovered peers and to a server, and advertising and listening for nearby peers via Bluetooth LE.

To organize them, I created 'Manager' singleton classes for each responsibility (e.g. LocationManager, BonjourManager, etc..) I start them when the app starts and stop them when the app terminates.

Obviously, I don't want them running in the background when the user is not interacting with the app. In iOS this was simple enough; each manager registers itself for lifecycle notifications, pausing on appDidEnterBackground and resuming on appDidEnterForeground.

My question is: How can I 'manage' these managers in Android so that they stop running when the app in not visible to the user and resume when the app is opened again? As far as I know, there is no global onPause and onResume which get called when the app switches between background and foreground.

I looked into using BoundServices but then I would need a binding between every activity and every manager so that the managers aren't destroyed until all activities unbound?

Help! I have a feeling that managers maybe aren't the right way to keep these activities alive...

joshblour
  • 1,024
  • 10
  • 19
  • It looks that there is no easy solution http://stackoverflow.com/questions/3667022/checking-if-an-android-application-is-running-in-the-background – Dmitry Ryadnenko Nov 06 '15 at 13:20
  • 1
    Maybe this can help http://stackoverflow.com/questions/20912229/android-application-level-pause-and-resume Something with keeping track of the onStart etc. count. – stonecompass Nov 06 '15 at 13:25
  • 1
    keep all your managers in a local service, in your activity `onStart` / `onStop` methods call `bindService` / `unbindService` (you need it anyway since you want to talk to your managers, right?), in your service when `onUnbind` is called it means that all of the clients have closed connection, means `onStop` has been called – pskink Nov 06 '15 at 14:36
  • @pskink, that would stop then quickly start all the singletons when I transition between activities or even when i rotate the screen. – joshblour Nov 06 '15 at 15:31
  • sure, then do "delayed shutdown" by using `Handler#postDelayed(this, 10 * 1000)` that way the real shutdown will take place after 10 seconds after `onUnbind` was called, and of course delete any pending messages when clients rebind – pskink Nov 06 '15 at 15:37
  • @DanielBross I ended up using https://github.com/curioustechizen/android-app-pause which I found through your link. If you post that comment as an answer, I'll accept it. – joshblour Nov 09 '15 at 14:15

2 Answers2

1

Maybe this can help http://www.stackoverflow.com/questions/20912229/… Something with keeping track of the onStart etc. count.

stonecompass
  • 547
  • 9
  • 27
  • 1
    It seems like activity counting is the only way. I started with my own implementation but eventually just switched to this library because of the way it handles screen rotations: http://kiranrao.in/android-app-pause/ – joshblour Nov 09 '15 at 14:29
-1

Init your services in Application class. Termination is handled "by the system"...

public void onTerminate ()

This method is for use in emulated process environments. It will never be called on a production Android device, where processes are removed by simply killing them; no user code (including this callback) is executed when doing so.

Community
  • 1
  • 1
Florian Barth
  • 1,392
  • 12
  • 25
  • Well, termination might never happen if the system doesn't need the resources. Or it might happen after a long delay. My goal here is not to waste the users battery by leaving these operations running when the user is not interacting with the app. – joshblour Nov 06 '15 at 14:02
  • This answer is completely out of target. First, this method will never be called on real devices. Second, OP doesn't need to know when process is terminated, he needs to know when app goes to background. – Dmitry Ryadnenko Nov 06 '15 at 14:05
  • 1
    If that is so, "Activity" counting (onPause, onResume) is your weapon of choice. – Florian Barth Nov 06 '15 at 14:16
  • Thanks @FlorianBarth. I was hoping there was an easier way or maybe even a better way to organize these operations (i.e. not singleton managers) – joshblour Nov 06 '15 at 14:20
  • You could use a bound service that manages all your singletons, this way you only need to connect all Activities with the one bound service. If all Activities are gone, it may shutdown the singletons. – Florian Barth Nov 06 '15 at 14:31