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

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

问题描述

提到这里,当屏幕熄灭,说明的onStop()将被调用。我需要检查开/关状态时,屏幕的的onStop()我的活动的被调用。所以我注册了一个的BroadcastReceiver 对这些行动( ACTION_SCREEN_ON ACTION_SCREEN_OFF )来记录开/关状态的电流(和他们正常工作,我已经登录!)。
但是当我关闭屏幕并检查的onStop ,它说的开/关状态屏幕上。为什么?我认为,接收器必须收到 ACTION_SCREEN_OFF 的onStop 被称为有啥错?

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 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()方法是去precated API级别21。您应该是isInteractive来代替:

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

boolean isScreenOn = pm.isInteractive();

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

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

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