3

I am writing a metronome app, I have the sound running from a service which I would like to keep running when the user is outside the app (browsing etc) and hasn't made a conscious effort to press the stop button.

It seems I should be using startForeground() as it's not the kind of thing you want in the background without paying attention to, and I really don't want the service to be killed at any time. Ideally I wouldn't be showing the notification while the app is in focus though. I notice that the Google Play Music app works this way, it only shows the notification when outside the app. Is anyone able to tell me how they have done this, I would find it hard to believe they wouldn't be using startForeground() after reading all the documentation stating you should be using it for this sort of app?

On the other hand, is this really desired behavior? It seems good to me, but I noticed that pretty much every other app I use with a long running service (HTC's music app, Navigation, few others) shows the notification icon at all times, whether in the app or not. I am interested in what is considered correct behavior. Any help much appreciated, thanks.

Anthony
  • 109
  • 2
  • 6

1 Answers1

1

Is anyone able to tell me how they have done this

They probably call stopForeground() when one of their activities returns to the foreground, and startForeground() when they think the user has left (e.g., onUserLeaveHint()).

I am interested in what is considered correct behavior.

I do not think there is a definitive "correct behavior" in this area. It is easier to do what the other apps do and leave the Notification around, until the user indicates that they no longer want the background work to run.

Note that while you may want "to keep running when the user is outside the app", the user might not. Certain types of apps (e.g., music players) can safely assume that the user wants the audio to keep going; a metronome, IMHO, does not rise to that level. Please allow the user to configure this behavior, such as through a SharedPreference.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Out of curiosity, what makes you suggest they might use `onUserLeaveHint()` rather than `onPause()` for this purpose? – Dan Hulme Apr 26 '13 at 17:34
  • Thanks, will look into the first thing if I decide to go ahead and keep it running. You have made me question if I actually should now, seems most of the metronomes on the play store do so but you may be right. Has the benefit of saving me work if I don't keep it running. If I do, I will definitely make it a preference. Thanks for the advice. – Anthony Apr 26 '13 at 17:35
  • @DanHulme: `onPause()` would also trigger when switching between their activities. While `onUserLeaveHint()` worries me (I'm not 100% convinced it covers all scenarios), it would avoid flickering the `Notification` as we switch between activities. Another possibility would be to use `onStop()`/`onStart()` and a reference count, as I *think* `onStart()` of the next activity would be called before `onStop()` of the previous one, and so you would just reinstate `startForeground()` when the reference count hit zero. – CommonsWare Apr 26 '13 at 17:44
  • I see what you mean. `onUserLeaveHint` wouldn't be called if the user leaves the app thanks to an incoming call, for instance. I hadn't considered that you might have more than one `Activity` where the notification shouldn't be shown. – Dan Hulme Apr 26 '13 at 17:52
  • @DanHulme: And it's those sorts of issues why I think most developers just leave `startForeground()` going, rather than trying to toggle it based on activity behavior. Lots simpler and more reliable, at the cost of having the icon around a bit more. – CommonsWare Apr 26 '13 at 18:29
  • @CommonsWare gives me a suggestion. I build a notification big view using RemoteView to control play/pause like this link (http://stackoverflow.com/questions/14508369/how-to-create-a-notification-similar-to-play-music-app-from-google) All are right but when i click device back button and out from the application click event(Play/Pause/Forward/Close) button doesn't work.Please help me. – Helal Khan Mar 11 '14 at 13:52