2

I have an app that has a service that plays music. This is working because the service just keeps on going (and thus playing).

However, when the user presses the home key in any activity I should be able to detect if my application is idle or not. I've been looking around and every question I find has an asnwer like: "use the onPause() or onStop()" but that only works for one activity and copying the code to every activity seems like a dirty solution.

In my main I have tried:

  1. onDestroy: doesn't work when home key pressed.
  2. onPause: stops the music even when starting another intent (not intended).

I have considered detecting the current activity from main but that's a way too dirty solution.

In my main activity I start my service (backgroundMusicPlayer):

@Override
protected void onCreate(Bundle savedInstanceState) {
    if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
        setContentView(R.layout.activity_main);
    else
        setContentView(R.layout.activity_main_landscape);
    //_________BRON(MediaPlayer): http://stackoverflow.com/questions/3369068/android-play-sound-on-button-click-null-pointer-exception
    backgroundMusicPlayer = new Intent(MainActivity.this, BackgroundMusicPlayer.class);
    startService(backgroundMusicPlayer);

    mp = MediaPlayer.create(MainActivity.this, R.raw.buttonclick); //knopgeluiden laden
    super.onCreate(savedInstanceState);
}

What should I do?

DerpyNerd
  • 4,743
  • 7
  • 41
  • 92
  • What do you do on onPause? You probably should call the public functions of MediaPlayer like start, pause, release when finished. Tommy K – The Original Android Oct 31 '13 at 23:31
  • That's the problem, you're probably suggesting calling stopService(intent) in my onPause method, but that would stop the service even when opening another activity inside my app. That's not my intention. The stopService(Intent) method should only be called when the app is stoppen or when minimized (by pressing the home key for example, but that can't be caught by any event for the newer and older API's). – DerpyNerd Oct 31 '13 at 23:35
  • I think you should accept the fact there is no listener for the HOME key, that is no HOME detection. Look at other apps like Pandora. You must press the Pause button to pause the music. Otherwise if you still persist on detecting the HOME, you can make your service check if your app is on foreground or not, periodically, as a workaround. – The Original Android Nov 01 '13 at 08:24
  • Hi, I already accepted the fact that listening to the home key is impossible. I never expected android apps had to be put together with workarounds all the time... – DerpyNerd Nov 01 '13 at 13:11

1 Answers1

0

What you need to know is when your activity is in the background (and when it has returned). There are a few ways to do this, but a reliable way I found was based on this answer.

It works on the premise that using a time reference between activity transitions will most likely provide adequate evidence that an app has been "backgrounded" or not.

This has worked for me and in the callback you can reliably tell your service to do whatever you want.

Also, if your App goes to the background and you're playing, you should then startForeground in your service (so it gains priority) and when the app returns, just stopForeground.

That way you avoid having a foreground service all the time, even when the user is interacting with the app.

Community
  • 1
  • 1
Martin Marconcini
  • 26,875
  • 19
  • 106
  • 144