我怎样才能看到哪些唤醒锁是活跃的

时间:2022-10-07 09:57:03

For some reason my Android phone won't go to sleep. I assume that a wakelock is keeping it awake, but there is no way to tell which wakelocks are active. The running services doesn't list anything suspicious, and certainly nothing different from usual. So my questions are:

出于某种原因,我的Android手机不会进入睡眠状态。我认为唤醒锁是让它保持清醒,但没有办法分辨哪些唤醒锁是活跃的。正在运行的服务没有列出任何可疑的东西,当然也没有什么不同。所以我的问题是:

  1. Does Android definitely release wakelocks when a process ends? Is it possible an app was badly written and didn't release a wakelock before exiting?

    当进程结束时,Android肯定会发布唤醒锁吗?有可能一个应用程序写得不好,并且在退出之前没有发布唤醒锁吗?

  2. Is there any way to see the active wakelocks?

    有没有办法看到活跃的唤醒锁?

This is what dumpsys power shows:

这就是dumpsys的力量显示:

$ dumpsys power
Power Manager State:
  mIsPowered=true mPowerState=0 mScreenOffTime=226093 ms
  mPartialCount=0
  mWakeLockState=
  mUserState=
  mPowerState=
  mLocks.gather=
  mNextTimeout=91922738 now=92136117 -213s from now
  mDimScreen=true mStayOnConditions=0
  mScreenOffReason=3 mUserState=0
  mBroadcastQueue={-1,-1,-1}
  mBroadcastWhy={0,0,0}
  mPokey=1 mPokeAwakeonSet=false
  mKeyboardVisible=false mUserActivityAllowed=false
  mKeylightDelay=6000 mDimDelay=47000 mScreenOffDelay=7000
  mPreventScreenOn=false  mScreenBrightnessOverride=-1  mButtonBrightnessOverride=-1
  mScreenOffTimeoutSetting=60000 mMaximumScreenOffTimeout=2147483647
  mLastScreenOnTime=0
  mBroadcastWakeLock=UnsynchronizedWakeLock(mFlags=0x1 mCount=0 mHeld=false)
  mStayOnWhilePluggedInScreenDimLock=UnsynchronizedWakeLock(mFlags=0x6 mCount=0 mHeld=false)
  mStayOnWhilePluggedInPartialLock=UnsynchronizedWakeLock(mFlags=0x1 mCount=0 mHeld=false)
  mPreventScreenOnPartialLock=UnsynchronizedWakeLock(mFlags=0x1 mCount=0 mHeld=false)
  mProximityPartialLock=UnsynchronizedWakeLock(mFlags=0x1 mCount=0 mHeld=false)
  mProximityWakeLockCount=0
  mProximitySensorEnabled=false
  mProximitySensorActive=false
  mProximityPendingValue=-1
  mLastProximityEventTime=0
  mLightSensorEnabled=false
  mLightSensorValue=-1.0 mLightSensorPendingValue=-1.0
  mLightSensorScreenBrightness=35 mLightSensorButtonBrightness=255 mLightSensorKeyboardBrightness=0
  mUseSoftwareAutoBrightness=true
  mAutoBrightessEnabled=false
  mScreenBrightness: animating=false targetValue=-1 curValue=0.0 delta=-1.3333334

mLocks.size=0:

mPokeLocks.size=1:
    poke lock 'PhoneApp': POKE_LOCK_IGNORE_CHEEK_EVENTS

7 个解决方案

#1


23  

In new versions of Android you can see list of wakelocks here:

在Android的新版本中,您可以在此处查看唤醒锁列表:

adb shell "cat /sys/kernel/debug/wakeup_sources"

#2


20  

You can use below adb command to require a wake lock

您可以使用以下adb命令来要求唤醒锁定

  adb shell "echo mylock > /sys/power/wake_lock"

Then, you can use below command to watch if this lock is active. You will see the time column continuously change, it means the wake lock is active

然后,您可以使用以下命令来监视此锁是否处于活动状态。您将看到时间列不断变化,这意味着唤醒锁定处于活动状态

  watch -n 1 'adb shell "cat /proc/wakelocks" | grep mylock'

Now, use this adb command to release the wake lock

现在,使用此adb命令释放唤醒锁定

  adb shell "echo mylock > /sys/power/wake_unlock"

Then, check it again, the time column will freeze, it means the wake lock is non active

然后,再次检查,时间列将冻结,这意味着唤醒锁定未激活

  watch -n 1 'adb shell "cat /proc/wakelocks" | grep mylock'

You can use the same technique to observe the wake lock you acquire in the code.

您可以使用相同的技术来观察您在代码中获取的唤醒锁定。

#3


14  

Does Android definitely release wakelocks when a process ends?

当进程结束时,Android肯定会发布唤醒锁吗?

I doubt it, though I do not know for certain.

我对此表示怀疑,尽管我不确定。

Is it possible an app was badly written and didn't release a wakelock before exiting?

有可能一个应用程序写得不好,并且在退出之前没有发布唤醒锁吗?

AFAIK, yes.

Is there any way to see the active wakelocks?

有没有办法看到活跃的唤醒锁?

Run adb shell dumpsys power.

运行adb shell dumpsys power。

#4


6  

As others have already said, adb shell dumpsys power can show you active wakelocks.

正如其他人已经说过的那样,adb shell dumpsys的强大功能可以向你展示活跃的唤醒锁。

However, for apps that didn't release wakelock, as per the answer here: https://*.com/a/16258624/428271 It should be released by Android. However, it is for case where app/process is killed, but it should be applicable when app/process is complete too. It also provides the source code references rather than just saying 'checked in code'

但是,对于没有发布唤醒锁的应用程序,请按照以下答案:https://*.com/a/16258624/428271它应该由Android发布。但是,它适用于app / process被杀死的情况,但是当app / process也完成时它应该适用。它还提供源代码引用,而不仅仅是说'签入代码'

#5


3  

You can check "Wakelock Detector" app which is available in Google Play

您可以查看Google Play中提供的“Wakelock Detector”应用

It has a simple UI which shows detail list of acquired wakelocks per each application and as you mentioned, it shows active running apps on the top, which might have wakelock present at the moment.

它有一个简单的用户界面,显示每个应用程序获取的唤醒锁的详细列表,正如您所提到的,它显示了顶部的活动运行应用程序,此时可能还有唤醒锁。

Wakelock detector

#6


2  

To see active wake-locks run :

要查看活动的唤醒锁定运行:

adb shell dumpsys power|grep -i wake

adb shell dumpsys power | grep -i wake

#7


0  

Android does not release wakelocks when the process ends. It should be explicitly released by the process before ending.

Android在此过程结束时不会释放唤醒锁。它应该在结束之前由流程明确释放。

#1


23  

In new versions of Android you can see list of wakelocks here:

在Android的新版本中,您可以在此处查看唤醒锁列表:

adb shell "cat /sys/kernel/debug/wakeup_sources"

#2


20  

You can use below adb command to require a wake lock

您可以使用以下adb命令来要求唤醒锁定

  adb shell "echo mylock > /sys/power/wake_lock"

Then, you can use below command to watch if this lock is active. You will see the time column continuously change, it means the wake lock is active

然后,您可以使用以下命令来监视此锁是否处于活动状态。您将看到时间列不断变化,这意味着唤醒锁定处于活动状态

  watch -n 1 'adb shell "cat /proc/wakelocks" | grep mylock'

Now, use this adb command to release the wake lock

现在,使用此adb命令释放唤醒锁定

  adb shell "echo mylock > /sys/power/wake_unlock"

Then, check it again, the time column will freeze, it means the wake lock is non active

然后,再次检查,时间列将冻结,这意味着唤醒锁定未激活

  watch -n 1 'adb shell "cat /proc/wakelocks" | grep mylock'

You can use the same technique to observe the wake lock you acquire in the code.

您可以使用相同的技术来观察您在代码中获取的唤醒锁定。

#3


14  

Does Android definitely release wakelocks when a process ends?

当进程结束时,Android肯定会发布唤醒锁吗?

I doubt it, though I do not know for certain.

我对此表示怀疑,尽管我不确定。

Is it possible an app was badly written and didn't release a wakelock before exiting?

有可能一个应用程序写得不好,并且在退出之前没有发布唤醒锁吗?

AFAIK, yes.

Is there any way to see the active wakelocks?

有没有办法看到活跃的唤醒锁?

Run adb shell dumpsys power.

运行adb shell dumpsys power。

#4


6  

As others have already said, adb shell dumpsys power can show you active wakelocks.

正如其他人已经说过的那样,adb shell dumpsys的强大功能可以向你展示活跃的唤醒锁。

However, for apps that didn't release wakelock, as per the answer here: https://*.com/a/16258624/428271 It should be released by Android. However, it is for case where app/process is killed, but it should be applicable when app/process is complete too. It also provides the source code references rather than just saying 'checked in code'

但是,对于没有发布唤醒锁的应用程序,请按照以下答案:https://*.com/a/16258624/428271它应该由Android发布。但是,它适用于app / process被杀死的情况,但是当app / process也完成时它应该适用。它还提供源代码引用,而不仅仅是说'签入代码'

#5


3  

You can check "Wakelock Detector" app which is available in Google Play

您可以查看Google Play中提供的“Wakelock Detector”应用

It has a simple UI which shows detail list of acquired wakelocks per each application and as you mentioned, it shows active running apps on the top, which might have wakelock present at the moment.

它有一个简单的用户界面,显示每个应用程序获取的唤醒锁的详细列表,正如您所提到的,它显示了顶部的活动运行应用程序,此时可能还有唤醒锁。

Wakelock detector

#6


2  

To see active wake-locks run :

要查看活动的唤醒锁定运行:

adb shell dumpsys power|grep -i wake

adb shell dumpsys power | grep -i wake

#7


0  

Android does not release wakelocks when the process ends. It should be explicitly released by the process before ending.

Android在此过程结束时不会释放唤醒锁。它应该在结束之前由流程明确释放。