开发者

Android: Resume from service to activity

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 s开发者_如何学Gotarts....Thanks.


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


Stop the service in OnResume(). Start the service in OnPause().


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"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜