简体   繁体   English

取决于屏幕关闭方式的不同行为

[英]Different behavior depending upon how screen is turned off

I am finding a weird situation depending upon how the screen is turned off. 根据屏幕的关闭方式,我发现一种奇怪的情况。 If Android turns off the screen or the power button is pressed to turn off the screen, my app as expected is paused . 如果Android关闭屏幕或按下电源按钮以关闭屏幕,则我的应用程序将paused When the power button is pressed to turn the screen back on, the app resumes no issues . 按下电源按钮以重新打开屏幕时,该应用不会 resumes 问题 This is true for a few moments or when tens of minutes pass. 几分钟或数十分钟后,情况就是如此。

Here's where it deviates. 这是它偏离的地方。 If my app turns the screen off , my app pauses as expected, but my app is also killed immediately. 如果my app turns the screen off ,我的应用程序pauses的预期,但我的应用程序也当即毙命

When the screen is turned back on with the power button, the app is no longer running 使用电源按钮重新打开屏幕时,该应用程序不再运行

I have set up logging that confirms this, but why the difference in behavior? 我设置了日志记录来确认这一点,但是为什么行为有所不同?

The behaviour you are seeing is actually expected, there's a priority for each of the processes running in an Android Device, once your application goes to pause it might become elegible by the OS to get rid of it if another process with higher priority requires memory, as per google's documentation: 您所看到的行为实际上是预期的,Android设备中运行的每个进程都有一个优先级,一旦您的应用程序暂停,如果另一个优先级更高的进程需要内存,则操作系统可能会删除它,根据谷歌的文档:

The Android system tries to maintain an application process for as long as possible, but eventually needs to remove old processes to reclaim memory for new or more important processes. Android系统尝试尽可能长时间地维护应用程序进程,但最终需要删除旧进程以为新的或更重要的进程回收内存。 To determine which processes to keep and which to kill, the system places each process into an "importance hierarchy" based on the components running in the process and the state of those components. 为了确定保留哪些进程以及杀死哪些进程,系统会根据进程中运行的组件以及这些组件的状态,将每个进程置于“重要性层次结构”中。 Processes with the lowest importance are eliminated first, then those with the next lowest importance, and so on, as necessary to recover system resources. 为了恢复系统资源,必须先删除重要性最低的进程,然后再删除重要性次低的进程,依此类推。

As explained before "based on the components running in the process" means that your application might not be highly ranked in the OS, since you might have not Service running, etc, hence it is being killed eventually... 如前所述,“基于进程中运行的组件”意味着您的应用程序在OS中的排名可能不高,因为您可能尚未运行Service等,因此最终被杀死。

You can look at this document: http://developer.android.com/guide/components/processes-and-threads.html , it explains the process life cycle and should give you a better understanding on what to do to deal with it... 您可以查看以下文档: http : //developer.android.com/guide/components/processes-and-threads.html ,它说明了流程生命周期,应该使您更好地理解如何处理它。 ...

Also in the battle to make battery last longer in Android devices there's a: Suspend/Resume state on the device. 在使电池在Android设备中使用更长时间的斗争中,还有一个:设备的挂起/恢复状态。 Suspend and resume happen in a three-step sequence and thus are best discussed together. 暂停和恢复按三个步骤进行,因此最好一起讨论。 Basically, when the device is suspended with the power button or the screen saver timeout expires, the device suspends. 基本上,当使用电源按钮将设备挂起或屏幕保护程序超时到期时,设备将挂起。 If the power button is pressed again, it resumes to the lock-screen. 如果再次按下电源按钮,它将恢复到锁定屏幕。 At this point, if the user unlocks the device, the application is resumed. 此时,如果用户解锁设备,则该应用程序将恢复。 If the user waits several seconds without unlocking the lock screen, the device will suspend again. 如果用户等待几秒钟而未解锁锁定屏幕,则设备将再次挂起。

Since this is explicitly a low-power state, the application should have stopped all rendering and sound, and likely any background processing that isn't 100% required to keep the app alive. 由于这显然是一种低功耗状态,因此该应用程序应该已经停止了所有渲染和声音,并且可能停止了不需要100%的任何后台处理即可保持该应用程序正常运行。

Regards! 问候!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果屏幕关闭,如何更新时钟小部件? - How to update clock widget if screen is turned off? 如何根据屏幕分辨率查看徽标? - how to view logo depending upon screen resolution? 有没有一种方法可以检查屏幕是打开还是关闭? - Is there a method to check if the screen is turned on or off? 如何在Android 5.0 Lollipop中关闭屏幕固定时收到通知? - How to be notified when screen pinning is turned off in Android 5.0 Lollipop? 在Android中关闭屏幕时如何防止CPU“休眠”? - How to keep CPU from 'sleeping' when screen is turned off in Android? 如何在屏幕关闭后保持播放有效 - How to keep playback active after the screen is turned off 即使在Android中关闭了屏幕,如何保持应用状态? - How to keep app status even if screen is turned off in Android? 如何锁定设备,但防止屏幕在android中关闭? - how to lock device but prevent the screen to be turned off in android? 屏幕打开/关闭时如何更新 Android 小部件? - How to update an Android widget when the screen is turned On/Off? 屏幕关闭时如何获取传感器数据? - How to get sensor data in service when screen is turned off?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM