Android|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
推荐阅读
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- android第三方框架(五)ButterKnife
- Android中的AES加密-下
- 宋仲基&宋慧乔(我们不公布恋情,我们直接结婚。)
- 带有Hilt的Android上的依赖注入
- 21天|21天|M&M《见识》04
- android|android studio中ndk的使用
- Android事件传递源码分析
- RxJava|RxJava 在Android项目中的使用(一)
- Android7.0|Android7.0 第三方应用无法访问私有库