Android 如何在屏幕关闭时保持活动运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11978742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:20:58  来源:igfitidea点击:

How to keep an Activity running while screen off?

android

提问by babysnow

How to keep an Activity running/active when the screen shuts off?

屏幕关闭时如何保持活动运行/活动?

回答by Anirudh Ramanathan

You will need to use a PARTIAL_WAKE_LOCKto ensure that your activity is kept active. android.permission.WAKE_LOCKmust be requested in your manifest. However, battery will drain faster, so do remember to releasethe wakelock as soon as possible.

您需要使用PARTIAL_WAKE_LOCK来确保您的活动保持活跃。android.permission.WAKE_LOCK必须在您的清单中请求。但是,电池会消耗得更快,因此请记住尽快释放唤醒锁。

Alternately, use a Serviceinstead

或者,使用服务代替

回答by Tom-Oliver Heidel

There is also a good way. I found this some months ago and it save a little bit the battery life ;)

还有一个好办法。几个月前我发现了这个,它可以节省一点电池寿命;)

http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/

http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/

回答by Engin Yapici

Be sure you don't have ' android:noHistory="true" ' in your manifest file for any of the activities. It kills the activity when the app goes off the screen or screen shuts off.

确保您的清单文件中没有任何活动的“ android:noHistory="true" ”。当应用程序关闭屏幕或屏幕关闭时,它会终止活动。

If you want your app stay active even if the screen is off I don't think you need to do anything extra: My application doesn't have any extra code for it and when I turn the screen off and on it is still there. But! Android OS probably kills it after a while when the screen is off, so you should probably use WAKE_LOCK in your manifest file. Or you can add ' android:keepScreenOn="true" ' to the manifest file to keep the screen ON all the time when your app is running.

如果您希望您的应用程序在屏幕关闭时保持活动状态,我认为您不需要做任何额外的事情:我的应用程序没有任何额外的代码,当我关闭和打开屏幕时它仍然存在。但!当屏幕关闭一段时间后,Android 操作系统可能会杀死它,因此您可能应该在清单文件中使用 WAKE_LOCK。或者,您可以将“ android:keepScreenOn="true" ”添加到清单文件中,以在您的应用程序运行时始终保持屏幕开启。