Android 锁屏&解锁&开屏监听

必须动态注册才可以监听到
必须动态注册才可以监听到
必须动态注册才可以监听到

  • 锁屏广播Action
Intent.ACTION_SCREEN_OFF
  • 解锁广播Action
Intent.ACTION_SCREEN_ON
  • 开屏广播Action
Intent.ACTION_USER_PRESENT

  • 广播ScreenBroadcastReceiver
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.example.util.LogUtil;

/**
 * @author master
 * @date 2018/1/23
 */

public class ScreenBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        LogUtil.e("广播Action = " + action);
        if (action.equals(Intent.ACTION_SCREEN_OFF)) {
            LogUtil.e("锁屏");
        } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
            LogUtil.e("解锁");
        }else if(action.equals(Intent.ACTION_USER_PRESENT)){
            LogUtil.e("开屏");
        }
    }
}

  • 注册广播(建议放在Service中注册)
        ScreenBroadcastReceiver screenBroadcastReceiver = new ScreenBroadcastReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_USER_PRESENT);
        getApplicationContext().registerReceiver(screenBroadcastReceiver, filter);

2018-01-26

你可能感兴趣的:(Android 锁屏&解锁&开屏监听)