android开发中如何捕捉自动休眠灰屏的这个事件?
休眠的过程是屏幕先变灰再变黑,想要实现在自动休眠灰屏时,点击后(屏幕会变亮)实现一些操作。试了以下两种方案:1、注册SCREEN_BRIGHTNESS广播,尝试获取屏幕的...
休眠的过程是屏幕先变灰再变黑,想要实现在自动休眠灰屏时,点击后(屏幕会变亮)实现一些操作。
试了以下两种方案:
1、注册SCREEN_BRIGHTNESS广播,尝试获取屏幕的变化,无果,应该是android自身没有发送对应的广播消息
2、点击时获取系统的亮度值,(通过oldBrightness = getInt(getContentResolver(), SCREEN_BRIGHTNESS);)但发现还是255,即还是亮,所以无法判断。
所以想问下有没有其他方法,还是我的实现有问题? 展开
试了以下两种方案:
1、注册SCREEN_BRIGHTNESS广播,尝试获取屏幕的变化,无果,应该是android自身没有发送对应的广播消息
2、点击时获取系统的亮度值,(通过oldBrightness = getInt(getContentResolver(), SCREEN_BRIGHTNESS);)但发现还是255,即还是亮,所以无法判断。
所以想问下有没有其他方法,还是我的实现有问题? 展开
2个回答
2015-12-07 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
捕捉自动休眠灰屏的事件,可以通过监听系统的广播获得当前手机屏幕的状态,广播需要在注册文件中注册,具体的实现代码如下:
public class ScreenListener {
private Context mContext;
private ScreenBroadcastReceiver mScreenReceiver;
private ScreenStateListener mScreenStateListener;
public ScreenListener(Context context) {
mContext = context;
mScreenReceiver = new ScreenBroadcastReceiver();
}
/**
* screen状态广播接收者
*/
private class ScreenBroadcastReceiver extends BroadcastReceiver {
private String action = null;
@Override
public void onReceive(Context context, Intent intent) {
action = intent.getAction();
if (Intent.ACTION_SCREEN_ON.equals(action)) { // 开屏
mScreenStateListener.onScreenOn();
} else if (Intent.ACTION_SCREEN_OFF.equals(action)) { // 锁屏
mScreenStateListener.onScreenOff();
} else if (Intent.ACTION_USER_PRESENT.equals(action)) { // 解锁
mScreenStateListener.onUserPresent();
}
}
}
/**
* 开始监听screen状态
*
* @param listener
*/
public void begin(ScreenStateListener listener) {
mScreenStateListener = listener;
registerListener();
getScreenState();
}
/**
* 获取screen状态
*/
private void getScreenState() {
PowerManager manager = (PowerManager) mContext
.getSystemService(Context.POWER_SERVICE);
if (manager.isScreenOn()) {
if (mScreenStateListener != null) {
mScreenStateListener.onScreenOn();
}
} else {
if (mScreenStateListener != null) {
mScreenStateListener.onScreenOff();
}
}
}
/**
* 停止screen状态监听
*/
public void unregisterListener() {
mContext.unregisterReceiver(mScreenReceiver);
}
/**
* 启动screen状态广播接收器
*/
private void registerListener() {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
mContext.registerReceiver(mScreenReceiver, filter);
}
public interface ScreenStateListener {// 返回给调用者屏幕状态信息
public void onScreenOn();
public void onScreenOff();
public void onUserPresent();
}
}
在上面的代码中,用到了动态注册广播,在需要监听屏幕状态的activity中,当不需要监听的时候,如activity finish的时候,使用unregisterListener解除广播监听;
下面是在Activity中调用;
ScreenListener l = new ScreenListener(this);
l.begin(new ScreenStateListener() {
@Override
public void onUserPresent() {
Log.e("onUserPresent", "onUserPresent");
}
@Override
public void onScreenOn() {
Log.e("onScreenOn", "onScreenOn");
}
@Override
public void onScreenOff() {
Log.e("onScreenOff", "onScreenOff");
}
});
通过以上步骤即实现了对屏幕状态的监听.
public class ScreenListener {
private Context mContext;
private ScreenBroadcastReceiver mScreenReceiver;
private ScreenStateListener mScreenStateListener;
public ScreenListener(Context context) {
mContext = context;
mScreenReceiver = new ScreenBroadcastReceiver();
}
/**
* screen状态广播接收者
*/
private class ScreenBroadcastReceiver extends BroadcastReceiver {
private String action = null;
@Override
public void onReceive(Context context, Intent intent) {
action = intent.getAction();
if (Intent.ACTION_SCREEN_ON.equals(action)) { // 开屏
mScreenStateListener.onScreenOn();
} else if (Intent.ACTION_SCREEN_OFF.equals(action)) { // 锁屏
mScreenStateListener.onScreenOff();
} else if (Intent.ACTION_USER_PRESENT.equals(action)) { // 解锁
mScreenStateListener.onUserPresent();
}
}
}
/**
* 开始监听screen状态
*
* @param listener
*/
public void begin(ScreenStateListener listener) {
mScreenStateListener = listener;
registerListener();
getScreenState();
}
/**
* 获取screen状态
*/
private void getScreenState() {
PowerManager manager = (PowerManager) mContext
.getSystemService(Context.POWER_SERVICE);
if (manager.isScreenOn()) {
if (mScreenStateListener != null) {
mScreenStateListener.onScreenOn();
}
} else {
if (mScreenStateListener != null) {
mScreenStateListener.onScreenOff();
}
}
}
/**
* 停止screen状态监听
*/
public void unregisterListener() {
mContext.unregisterReceiver(mScreenReceiver);
}
/**
* 启动screen状态广播接收器
*/
private void registerListener() {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
mContext.registerReceiver(mScreenReceiver, filter);
}
public interface ScreenStateListener {// 返回给调用者屏幕状态信息
public void onScreenOn();
public void onScreenOff();
public void onUserPresent();
}
}
在上面的代码中,用到了动态注册广播,在需要监听屏幕状态的activity中,当不需要监听的时候,如activity finish的时候,使用unregisterListener解除广播监听;
下面是在Activity中调用;
ScreenListener l = new ScreenListener(this);
l.begin(new ScreenStateListener() {
@Override
public void onUserPresent() {
Log.e("onUserPresent", "onUserPresent");
}
@Override
public void onScreenOn() {
Log.e("onScreenOn", "onScreenOn");
}
@Override
public void onScreenOff() {
Log.e("onScreenOff", "onScreenOff");
}
});
通过以上步骤即实现了对屏幕状态的监听.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询