码迷,mamicode.com
首页 > 其他好文 > 详细

屏幕锁屏以及解锁监听

时间:2015-05-05 19:22:16      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:

屏幕锁屏以及解锁时会分别发送两个广播SCREEN_ON和SCREEN_OFF,但是这两个action只能通过代码的形式注册才能被监听到,在AndroidManifest.xml中注册根本监听不到。

 

public class ScreenActionReceiver extends BroadcastReceiver {

    private String TAG = "ScreenActionReceiver";
    private boolean isRegisterReceiver = false;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(Intent.ACTION_SCREEN_ON)) {
            Logcat.d(TAG, "屏幕解锁广播...");
        } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
            Logcat.d(TAG, "屏幕加锁广播...");
        }
    }

    public void registerScreenActionReceiver(Context mContext) {
        if (!isRegisterReceiver) {
            isRegisterReceiver = true;

            IntentFilter filter = new IntentFilter();
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            filter.addAction(Intent.ACTION_SCREEN_ON);
            Logcat.d(TAG, "注册屏幕解锁、加锁广播接收者...");
            mContext.registerReceiver(ScreenActionReceiver.this, filter);
        }
    }

    public void unRegisterScreenActionReceiver(Context mContext) {
        if (isRegisterReceiver) {
            isRegisterReceiver = false;
            Logcat.d(TAG, "注销屏幕解锁、加锁广播接收者...");
            mContext.unregisterReceiver(ScreenActionReceiver.this);
        }
    }
}

 

屏幕锁屏以及解锁监听

标签:

原文地址:http://www.cnblogs.com/huangzx/p/4479666.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!