简体   繁体   English

当手机进入睡眠/待机模式时,活动/服务会发生什么?

[英]What happens to Activities/Services when phone goes to sleep/standby mode?

当手机/ AP进入睡眠模式时,Android应用程序以及属于应用程序的活动和服务会发生什么?框架会破坏活动和服务,操作系统会杀死进程吗?

In case of device sleep, activity's `onPause()' will be called. 在设备休眠的情况下,将调用活动的`onPause()'。 Read activity lifecycle to understand this. 阅读活动生命周期以了解这一点。

OS only kills the process when memory/resources are low. 操作系统仅在内存/资源不足时才会终止进程。 Activities are killed first, services are only killed as last resort. 活动首先被杀死,服务只是作为最后的手段被杀死。

But there is no guarantee they will not be killed. 但不能保证他们不会被杀死。 This is why you should rely on system services to call you when you need some work done: use AlarmManager to do call your code periodically or use listeners to notify you of system changes (gps, network, etc..) 这就是为什么当你需要完成一些工作时你应该依赖系统服务来打电话给你:使用AlarmManager定期调用你的代码或使用监听器来通知你系统的变化(gps,网络等......)

When the phone sleeps activities don't get destroyed. 当手机睡觉活动时不要被破坏。 I believe all that happens is the activities stay the same but fire the onPause() method. 我相信所发生的一切都是活动保持不变但是激活了onPause()方法。

See this image: 看这个图片:

替代文字

What i saw in my application is that only the onPause() method of the main activity (category.LAUNCHER) is called. 我在我的应用程序中看到的是,只调用主活动(category.LAUNCHER)的onPause()方法。 This happened when the phone went to sleep and before that the main activity of the application had been started. 这种情况发生在手机进入睡眠状态时,之前应用程序的主要活动已经开始。

When any one of the other activities had been started before the phone was going to sleep the first onPause() is called then onStop() and in the end onDestroy() - this is for activities which are category.DEFAULT into the manifest. 当手机进入睡眠状态之前已启动任何其他活动时,第一个onPause()被调用,然后是onStop(),最后是onDestroy() - 这是针对manifest.DEFAULT进入清单的活动。

I don't know maybe the problem is in my code? 我不知道问题可能在我的代码中?

When the phone goes to sleep the onPause() method is called. 当手机进入休眠状态时,会调用onPause()方法。 This method is just a warning to your app. 此方法只是对您的应用的警告。 Then depending on the device the CPU may also go to sleep and execution of your code may stop. 然后,根据设备,CPU也可能会进入休眠状态,代码的执行可能会停止。 On most devices this may be anywhere from 10 to 60 seconds after the screen goes black. 在大多数设备上,这可能是屏幕变黑后10到60秒的任何时间。

It is very unlikely that going to sleep will result in your app being killed. 进入睡眠状态不太可能导致您的应用被杀。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM