美文网首页
Android 锁屏&解锁&开屏监听

Android 锁屏&解锁&开屏监听

作者: 兜里揣着酱油钱 | 来源:发表于2018-01-26 19:03 被阅读0次
必须动态注册才可以监听到
必须动态注册才可以监听到
必须动态注册才可以监听到

  • 锁屏广播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 锁屏&解锁&开屏监听

      本文链接:https://www.haomeiwen.com/subject/iivwaxtx.html