2

I read Anndroid document - http://developer.android.com/guide/topics/media/mediaplayer.html

which said ".. you want it to continue playing while the user is interacting with other applications—then you must start a Service and control the MediaPlayer instance from there. "

But I found if I start a local a music file from an activity, then leave that activity, ( for example, press HOME key and interact with another app ), the music continues playing.

So, I don't understand " the "must start a Service" in document. Did I miss something?

This was not a big obstacle for my app at this moment. I am just wondering what's the potential problems could be if I do not use Service.( Services have longer lifespan, so the mediaplayer could be killed earlier, any others ? )

Our development is based on Android 2.2.

Thanks for any help in advance.

san.yisi
  • 21
  • 1

3 Answers3

2

Big reason, if you are not using a service, users cannot listen to music outside of your app when the the activity gets paused or terminated. Technically you can make a music app but if your users cant listen to music when another app is in the foreground or the phone is in a different state(locked) it wont make for a very good app. You should take a look at the activity lifecycle for a deeper understanding of the process. Note that this behavior is by design for saving power, memory and cpu cycles.

It helps also not to think of services in the more traditional desktop dev usage. You want this thing to live even when your activity is not up and about.

For more about activity life cycles Managing the Activity Lifecycle

For the How http://www.androidcompetencycenter.com/2009/01/basics-of-android-part-iii-android-services/

For the why Why is it important to use Services for background tasks?

Community
  • 1
  • 1
Terrance
  • 11,764
  • 4
  • 54
  • 80
2

Playing the music in the Activity might be fine for now, but when Android is low on resources it might try to kill your Activity. When you add a service to an app, Android will try to keep that process alive as long as possible if it falls under certain criteria (such as playing music). Read over the Process Lifecycle section on Services:

http://developer.android.com/reference/android/app/Service.html#ProcessLifecycle

telkins
  • 10,440
  • 8
  • 52
  • 79
1

The activity can get killed by Android or by the user and then the music would stop playing.

Ran
  • 4,117
  • 4
  • 44
  • 70
  • 1
    It's my frist question, I really appreciate all answers. I have read some links, so the root, and the only reason, is that the Android deal with background Activity and Services in different way. The service has the longer time span or much less chances to be killed. It has no matter with implementation and Android version. Many thanks! – san.yisi Jun 18 '12 at 19:39