如何在 onStop() 中检查屏幕开/关状态? [英] how to check screen on/off status in onStop()?

查看:32
本文介绍了如何在 onStop() 中检查屏幕开/关状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处所述,当屏幕熄灭时,当前的onStop()活动将被调用.当我的 ActivityonStop() 被调用时,我需要检查屏幕的开/关状态.所以我已经为这些操作注册了一个 BroadcastReceiver(ACTION_SCREEN_ON AND ACTION_SCREEN_OFF)来记录当前的开/关状态(它们工作正常,我已登录!).
但是当我关闭屏幕并检查 onStop 中的开/关状态时,它说屏幕已打开.为什么?我认为接收器必须在 onStop 被调用之前收到 ACTION_SCREEN_OFF 那么有什么问题?

as mentioned here, when the screen goes off, the onStop() of current Activity will be called. I need to check the screen on/off status when the onStop() of my Activity is called. so I have registered a BroadcastReceiver for these actions(ACTION_SCREEN_ON AND ACTION_SCREEN_OFF) to record the current on/off status(and they work properly, I have logged!).
but when I turn off the screen and check the on/off status in the onStop , it says the screen is on. why? I think the receiver must receive the ACTION_SCREEN_OFF before onStop is called so what's wrong?

推荐答案

为此您可以尝试使用 PowerManager 系统服务,这里是示例和 官方文档(注意这个方法是在API level 7中添加的):

You can try to use PowerManager system service for this purpose, here is example and official documentation (note this method was added in API level 7):

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();

isScreenOn() 方法在 API 级别 21 中已弃用.您应该改用 isInteractive:

isScreenOn() method is deprecated API level 21. You should use isInteractive instead:

boolean isScreenOn = pm.isInteractive();

http://developer.android.com/reference/android/os/PowerManager.html#isInteractive()

这篇关于如何在 onStop() 中检查屏幕开/关状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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