苏醒锁禁用前台服务打盹模式 - 新的电池优化Android中中号 [英] Wake lock disabled in foreground service with Doze mode - new battery optimizations in Android M

查看:560
本文介绍了苏醒锁禁用前台服务打盹模式 - 新的电池优化Android中中号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是显然的半记录的行为,如果A G从戴安娜Hackborn评论+后构成记录:的 https://plus.google.com/+AndroidDevelopers/posts/94jCkmG4jff 但与通知图标关联的前台服务,应允许在打盹模式,保持唤醒锁。

看来这是无效的,当你有一个顶级的活动,除了前台的服务。

我已创建一个最小的实现,它说明了这一点: 的https://github.com/petrnalevka/dozetest/blob/master/src/com/urbandroid/doze/DozeService.java

该问题可以被再现上
Android的男,MRA58K的Nexus 5和MRA58N的Nexus 6

我的前台服务都有一个相关的通知和我持有部分唤醒锁。不幸的是打盹模式接管和我的唤醒锁坏了。我只能够通过选择退出的电池优化prevent这或离开顶。

我相信这是Android的一个错误,因为没有理由前台服务应保持唤醒锁,但不是他们是否有在上面的活动。

我把这个问题上所以即使我已经报告了这个放在这里Androdi问题跟踪器的 HTTPS://$c$c.google.com/p/android/issues/detail ID = 193802 以便找到解决办法,因为这是为了一个crutial功能?不需要REQUEST_IGNORE_BATTERY_OPTIMIZATIONS这显然仅仅意味着REQUEST_REMOVAL_FROM_PLAYSTORE_BY_GOOGLE的时刻。

下面是我如何能够重现使用亚行多亏了+戴安娜Hackborn提示问题:

看来,当我把一个活动的前景,同时也有运行我的应用程序的前台服务由电源管理器识别为:

  PARTIAL_WAKE_LOCK打盹锁已禁用(UID = 10112,PID = 24649,WS = NULL)
PROC#0:前F / A / TS TRM:0 24649:com.urbandroid.doze / u0a112(顶级活动)
 

如果我preSS家庭和离开活动我进入状态:

  PARTIAL_WAKE_LOCK打盹锁(UID = 10112,PID = 24649,WS = NULL)
PROC#4:PRCP F / S / SF TRM:0 24649:com.urbandroid.doze / u0a112(FG-服务)
 

解决方案

这是戴安娜Hackborn贴下的 https://plus.google.com/+AndroidDevelopers/posts/94jCkmG4jff 。仍然我们可以在这里集体讨论更多的解决方法作为分离过程可能不总是简单的。

戴安娜Hackborn: +切赫Nalevka对不起是的,这似乎像在平台上的错误。我会考虑得到它固定在可能的情况。

您的解决办法是在正确的方向,但请不要做那种事有开/关移动你的状态在屏幕上。嗯,这是好的,移动到后面,这样做,当屏幕关闭......但是,这是非常错误的,强迫自己的前面,当屏幕打开,因为它可能会打开其他原因(以发射摄像机,语音交互等)。这实际上可能是一些平台应该是在保护自己不受更好。

我的建议是侦听打盹模式入手,只是把你的活动到后面在这种情况下。为此,您可以通过侦听广播<一href="http://developer.android.com/reference/android/os/PowerManager.html#ACTION_DEVICE_IDLE_MODE_CHANGED" rel="nofollow">http://developer.android.com/reference/android/os/PowerManager.html#ACTION_DEVICE_IDLE_MODE_CHANGED并给自己发送到后面,当你看到设备闲置是真实的。我建议只是不担心试图让自己回到前台 - 在用户实际使用他们的设备很长一段时间一直没有的情况下发生此问题,回来,并在国内是不应该是一个惊喜。

EDIT(由戴安娜Hackborn):其实,另一种解决方案 - 让您的前台服务运行在不同的进程比活动。从我所看到的,这将很好地工作。如果你得到所需的行为出现,我会在看到有意思的。

另外这实际上是我们针对这种情况建议的做法 - 如果你有一个长期运行的前台服务,应当从活动的一个独立的进程,因此它不强制所有与该活动有关的记忆要保持周围。 (这也是为什么这个bug得到了通过,我们所有的应用程序使用这种模式。)

This is apparently semi-documented behavior, if comments from Dianne Hackborn on a G+ post constitute "documented": https://plus.google.com/+AndroidDevelopers/posts/94jCkmG4jff but foreground services associated with a notification icon should be allowed to keep a wake lock during doze mode.

It seems this is not valid when you have a top-activity in addition to the foreground service.

I have create a minimal implementation which demonstrates this: https://github.com/petrnalevka/dozetest/blob/master/src/com/urbandroid/doze/DozeService.java

The problem can be reproduced on
Android M, MRA58K Nexus 5 and MRA58N Nexus 6

My foreground service has an associated notification and I hold a partial wake lock. Unfortunately Doze mode takes over and my wake lock is broken. I was only able to prevent this by an opt-out from battery optimization or leaving the top activity.

I believe this is a bug in Android as there is no reason why foreground services should keep the wake lock but not if they have an activity on top.

I'm putting this issue on SO even I have already reported this on Androdi issue tracker here https://code.google.com/p/android/issues/detail?id=193802 in order to find workarounds as this is a crutial feature in order to not need the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS which apparently only mean REQUEST_REMOVAL_FROM_PLAYSTORE_BY_GOOGLE at the moment.

Here is how I was able to reproduce the issue using adb thanks to hints from +Dianne Hackborn:

It seems that when I keep an Activity in the foreground while having also the foreground service running my app is recognized by the Power manager as:

PARTIAL_WAKE_LOCK              'Doze lock' DISABLED (uid=10112, pid=24649, ws=null)  
Proc # 0: fore  F/A/TS trm: 0 24649:com.urbandroid.doze/u0a112 (top-activity)  

If I press home and leave the activity I get into the state:

PARTIAL_WAKE_LOCK              'Doze lock' (uid=10112, pid=24649, ws=null)  
Proc # 4: prcp  F/S/SF trm: 0 24649:com.urbandroid.doze/u0a112 (fg-service)  

解决方案

This is an answer by Dianne Hackborn posted under https://plus.google.com/+AndroidDevelopers/posts/94jCkmG4jff. Still we can brainstorm more workarounds here as separating the processes may not always be straightforward.

Dianne Hackborn: +Petr Nalevka Sorry yes this does seem like a bug in the platform. I will look into getting it fixed when possible.

Your work-around is in the right direction, but please don't do that kind of thing with moving your state in screen on/off. Well, it is okay to move to the back and do that when the screen turns off... however, it is very wrong to force yourself to the front when the screen turns on, because it could be turning on for other reasons (to launch camera, voice interaction, etc). This is actually probably something the platform should be better at protecting itself against.

What I would suggest is listening for doze mode to start, and just putting your activity to the back in that case. You can do that by listening for the broadcast http://developer.android.com/reference/android/os/PowerManager.html#ACTION_DEVICE_IDLE_MODE_CHANGED and sending yourself to the back when you see that device idle is true. I would suggest just not worrying about trying to get yourself back to the foreground -- in the case where the user actually hasn't been using their device for a long time and this happens, coming back to it and being in home shouldn't be a surprise.

EDIT (by Dianne Hackborn): Actually, another solution -- have your foreground service run in a different process than the activity. From what I can see, this will work fine. I would be interesting in seeing if you get the desired behavior there.

Also this is actually our recommended practice for this situation -- if you have a long-running foreground service, it should be in a separate process from the activity, so it doesn't force all of the memory associated with the activity to be kept around. (This is also why this bug got through, all of our apps use this pattern.)

这篇关于苏醒锁禁用前台服务打盹模式 - 新的电池优化Android中中号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆