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:
- onDestroy: doesn't work when home key pressed.
- 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?