I have an activity and one service. When i press the home button the activity paused and the service starts. I want, when i starts again my application to stops the service and my activity starts....Thanks.
Asked
Active
Viewed 2,124 times
3 Answers
0
If you don't want/can't change the design of your app, you must use this flag with your Intent in your service:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
If you don't want a new instance of your Activity, set launchMode for your Activity in the AndroidManifest file:
android:launchMode="singleTask"

Pelanes
- 3,451
- 1
- 34
- 32
0
Stop the service in OnResume(). Start the service in OnPause().

DeeV
- 35,865
- 9
- 108
- 95
-
-
-
That's what services are generally built for. http://stackoverflow.com/questions/4606141/getting-data-from-the-service-to-the-activity – DeeV Aug 01 '11 at 13:02
0
Change your design.
There is no need to switch between playing the music in an Activity and in a Service. Instead you should keep playing in the Service, and continually update the Activity from there using for example BroadcastReceivers.
These broadcastreceivers should be registered in the activity's OnResume and unregistered in the activity's OnPause.
See Using a Service with MediaPlayer from the official android documentation

Marmoy
- 8,009
- 7
- 46
- 74
-
Yes, but how service will inform my activity (when my app is on foreground), in order to ,for example update my slider. Can you give me an example? – Tim Aug 01 '11 at 15:06
-
Use broadcast messages from the service to the activity using broadcastreceiver. Here is an example: http://www.websmithing.com/2011/02/01/how-to-update-the-ui-in-an-android-activity-using-data-from-a-background-service/ – Marmoy Aug 01 '11 at 15:20
-
Ok, it works for me, now i have a button in my activity (play-pause), how i can send infos in my service in order to start-pause my player? Thanks.. – Tim Aug 01 '11 at 17:00
-