Android 监听屏幕是否锁屏的实例代码
作者:bzlj2912009596 时间:2022-01-19 14:31:24
今天,简单讲讲如何监听手机屏幕是否锁屏。
实现方法:
1)通过BroadcastReceiver接收广播Intent.ACTION_SCREEN_ON和Intent.ACTION_SCREEN_OFF可以判断屏幕状态是否锁屏,但是只有屏幕状态发生改变时才会发出广播;
2)如果要在屏幕状态发生改变之前就想获取屏幕状态,可以通过反射机制调用PowerManager的isScreenOn方法 。
具体实现,见代码:
直接上代码:
1.定义一个接收广播的类
package com.app.lib;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.PowerManager;
/**
* Created by ${zyj} on 2016/6/21.
*/
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();
}
}
2、使用:
package com.app.lib;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private ScreenListener screenListener ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
screenListener = new ScreenListener( MainActivity.this ) ;
screenListener.begin(new ScreenListener.ScreenStateListener() {
@Override
public void onScreenOn() {
Toast.makeText( MainActivity.this , "屏幕打开了" , Toast.LENGTH_SHORT ).show();
}
@Override
public void onScreenOff() {
Toast.makeText( MainActivity.this , "屏幕关闭了" , Toast.LENGTH_SHORT ).show();
}
@Override
public void onUserPresent() {
Toast.makeText( MainActivity.this , "解锁了" , Toast.LENGTH_SHORT ).show();
}
});
}
}
总结
以上所述是小编给大家介绍的Android 监听屏幕是否锁屏的实例代码网站的支持!
来源:http://blog.csdn.net/bzlj2912009596/article/details/77940983
标签:android,监听,屏幕
0
投稿
猜你喜欢
Java多态的全面系统解析
2023-06-22 13:39:22
java虚拟机原理:Class字节码二进制文件分析
2022-02-06 09:02:33
flutter的导航和路由使用示例详解
2023-11-21 07:06:34
Python爬虫之爬取2020女团选秀数据
2023-02-08 13:39:19
Spring创建bean实例的几种方式分享
2022-02-21 22:08:19
kotlin java 混合代码 maven 打包实现
2023-04-09 13:33:41
如何修改FeginCilent定义的服务名到指定服务
2022-07-05 05:49:05
用C#将图片保存至Oracle BLOB字段中的方法
2023-06-12 01:29:16
Windows10系统下JDK1.8的下载安装及环境变量配置的教程
2022-03-18 18:14:03
C#基础:基于const与readonly的深入研究
2023-09-26 14:22:30
C#实现绑定Combobox的方法
2023-02-16 03:49:48
Flutter如何轻松实现动态更新ListView浅析
2023-05-23 09:32:37
Spring框架构造注入type属性实例详解
2021-10-04 19:45:19
浅谈MyBatis中@MapKey的妙用
2023-09-25 10:42:02
Android开发-之五大布局详解
2021-11-30 14:24:30
java并发编程专题(一)----线程基础知识
2021-10-30 00:28:42
理解Java中的静态绑定和动态绑定
2022-05-28 04:20:06
C#实现读写ini文件类实例
2023-09-06 18:47:00
Flutter实现笑嘻嘻的动态表情的示例代码
2023-02-13 21:39:51
Android学习教程之圆形Menu菜单制作方法(1)
2022-05-23 03:08:50