本篇文章给大家分享的是有关Android中怎么监听屏幕是否锁屏,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
实现方法:
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; public class ScreenListener { private Context mContext; private ScreenBroadcastReceiver mScreenReceiver; private ScreenStateListener mScreenStateListener; public ScreenListener(Context context) { mContext = context; mScreenReceiver = new ScreenBroadcastReceiver(); } 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(); } } } public void begin(ScreenStateListener listener) { mScreenStateListener = listener; registerListener(); getScreenState(); } 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(); } } } public void unregisterListener() { mContext.unregisterReceiver(mScreenReceiver); } 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中怎么监听屏幕是否锁屏,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注编程网行业资讯频道。