سؤال

I've got a bit of a dilemma and not quite sure how to solve it. Here's the scenario...

I have a multi activity application which plays music from the time it starts to the time the application exits.

However, if I use onPause / onResume to detect when the activity is sent to the background and pause the music in onPause and resume play in onResume, the music "skips" briefly when I start the next activity as the calling activity is finished once the startActivity() is called.

If I don't pause / resume the music in onPause / onResume the music plays smoothly but does NOT stop if the home key is pressed and the activity is sent to the back.

Is there a way to detect an activity is sent to the background (using, say a timer and application flag) without having to use onPause / onResume?

If this is not possible or too hard to implement (I'm still learning as we all are), is there a way to create an "invisible" launcher activity which runs in the background to handle such things but never seen?

As always, thanks in advance.

هل كانت مفيدة؟

المحلول 5

OK, here's the solution and it's fairly straightforward once I thought about it in a more logical way.

What I have done is:-

  1. Create a public static int called activityCount.
  2. In the onCreate function of each activity I increment activityCount by 1.
  3. I @Override public void finish() and decrement activityCount by one and call super.finish().
  4. In onPause if activityCount == 1, pause the music.
  5. In onResume if activityCount == 1, play the music.

This is giving me the desired effect by continuously playing the music but when the home button is clicked the music stops and resumes when the activity is resumed.

Thanks for all the suggestions as it helped me think more logically.

نصائح أخرى

Turns out there's no simple way around this.

First thing you should do, is move your music streaming to a service, this way it doesn't depend on any activity.

Then you need to tell the service to stop when the entire application is in the background, not when one activity is paused. this fine answer suggests adding a timer to your application, and wait for a couple of seconds after an activity is paused. Of no other activity in the app is resumed - the app is assumed to be in the background.

I wouldn't add a timer to your application, but rather let your music playing service do this (notify the service in each onPause and onResume). Also, two seconds is too long for playing music, I'd start with 500ms and see if it's acceptable.

It's pretty simple IMHO

What you are trying to figure is if the new activity is from your application, continue playing the music, else stop.

You can do this with a boolean flag.

Here's the algorithm:

-boolean flag is set to true in onResume in all activities
-Keep the statements which pause the music in onPause
-Put the above statements (in onPause) in an if(boolean flag)
-whenever an event happens to start a new activity i.e. a button is clicked in your activities, etc, clear the flag (set it to false) in the event listener - eg: the button's onClickListener

This should work

The changeover between activities looks like this:

Activity A onPause
Activity B onCreate (or onRestart if it's already created)
Activity B onStart
Activity B onResume
Activity A onStop

You have a few options how to take advantage of this. You can subclass Application, and keep a boolean in the application class. Make a base Activity that does the following:

  1. In onPause(), call the application to change the boolean to false.
  2. In onResume(), call the application to change the boolean to true.
  3. In onStop(), call the application to stop the music unless the boolean is set to true.

If all of your Activities extend this base Activity, then this will work. When A stops, as long as B was another one of your Activities, it will have resumed and set the boolean to true, so your music will keep playing.

There's a second approach that uses a Bound Service, where you bind in onStart() and unbind in onStop(). You can explore that on your own if you so desire.

You can (and in the fact should) use service to handle the music player. This is the component you are calling as "invisible activity". If you start it by bindService() method in onResume and unbind in onPause() of each activity in your application it should run all the time. When no activity is bound to the running service the service is stopped by system, so all you need is just stop the music in the onDestroy() or in the onUnbind() method of the service.

Here you have a nice diagram of the Service lifecycle: http://www.tutorialspoint.com/android/android_services.htm

I'm not 100% if automatic management of servers lifecycle will be enough - in such case you can use startService() method to keep the service working all the time, when onUnbind of service is called put some delayed (i.e. using Handler.postDelayed(Runnable r) ) check if after i.e. after 1s service is still unbound and stopSelf() in such case.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top