[英]How do I know when application is in the background?
I have an application that is a menu with several options, every option when chosen will lead to a new activity. 我有一个应用程序,它是一个带有多个选项的菜单,选择每个选项都会导致新的活动。 I also have music playing in the background using a service. 我还使用服务在后台播放音乐。 I want my music play continuously no matter what activity is focused. 无论我专注什么活动,我都希望音乐能够连续播放。
My problem is: 我的问题是:
So what should I do now? 那我现在该怎么办?
You can override onPause()
and use a boolean flag. 您可以重写onPause()
并使用一个布尔标志。
Set the boolean to true
if you are launching a new activity. 如果要启动新活动,请将布尔值设置为true
。 When onPause()
is called you'll know if it is not true
then you should stop the music. 调用onPause()
,您会知道它是否为true
然后应该停止播放音乐。
boolean keepPlaying = false;
protected void onPause() {
if(!keepPlaying)
stopService(new Intent(this, MediaPlayerServices.class));
super.onPause();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.