EDIT - FOUND A EASY 5-10 LINE SOLUTION!!! See MY OWN ANSWER BELOW!!! YAY!!!!!!!!!
I\'ve searched for 5 hours, dozens of SO posts, no answers, and this seems like t
I faced same problem. I will tell you my solution. I overrided onStop, onStart each activity and counted my started activity with some delay and count reaches zero, I paused music.
how is it about un/register as a listener @ the service in onStart()
and onStop()
method of your activities and every time a listener is (un)registered your service checks if it's got some listeners left in its ArrayList<Listener>
. if the listOfListeners.size()
is 0
then your service calls its stopSelf()
method and kills itself.
at least that's how I would implement this...
so, the things you would need are:
2 Interfaces (Observer
, Observable
)
implement Observer
in Activity
, Observable
in Service
ArrayList<Observer>
in Service
registerObserver(Observer o)
and unregisterObserver(Observer o)
in Service
but that's just my point of view...
Why don't you just make a button to stop the music its so much easier and you don't have to force close everytime
Button exit =(Button) findViewById(R.id.exit);
exit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
// TODO: Implement this method
startActivity(new Intent("com.justme.worldexplorer.EXIT"));
finish();
Music I'd.stop();}});}
There you go and so the key hardware button back dosent leave if you press by accident
public boolean onKeyDown(int KeyCode,KeyEvent event){
return false;
}
And that's it set it up in the manifest and run if any problems let me know