android屏幕锁定,解锁,开屏比较常用,故抽象成一个公共的DeviceScreenListener。
在使用使用,首先new出一个DeviceScreenListener,然后注册(register)使用,使用后不要忘记注销,一般在生命周期的onDestory里面unregister。
抽象的DeviceScreenListener:
package zhangphil.app; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; /** * Created by Phil on 2017/7/18. */ /** * 对于开发者来说,可能更多需要关注ScreenStateListener中的两个函数: * void onScreenOff(); 屏幕锁定 * void onUserPresent(); 屏幕处于解锁状态且可以正常使用 */ public class DeviceScreenListener { private Context mContext; private ScreenBroadcastReceiver mScreenBroadcastReceiver; private ScreenStateListener mScreenStateListener; public DeviceScreenListener(Context context) { mContext = context; mScreenBroadcastReceiver = 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(); } } } /** * 开始监听屏幕开/关状态 * * @param listener */ public void register(ScreenStateListener listener) { mScreenStateListener = listener; /** * 注册屏幕设备开屏/锁屏的状态监听 */ IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_USER_PRESENT); mContext.registerReceiver(mScreenBroadcastReceiver, filter); //initScreenState(); //可选 } /** * 代码启动阶段获取设备屏幕初始状态 */ /** private void initScreenState() { PowerManager manager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); if (manager.isScreenOn()) { if (mScreenStateListener != null) { mScreenStateListener.onScreenOn(); } } else { if (mScreenStateListener != null) { mScreenStateListener.onScreenOff(); } } } */ /** * 注销屏幕设备开屏/锁屏的状态监听 */ public void unregister() { mContext.unregisterReceiver(mScreenBroadcastReceiver); mScreenBroadcastReceiver = null; mScreenStateListener = null; } interface ScreenStateListener { /** * 此时屏幕已经点亮,但可能是在锁屏状态 * 比如用户之前锁定了屏幕,按了电源键启动屏幕,则回调此方法 */ void onScreenOn(); /** * 屏幕被锁定 */ void onScreenOff(); /** * 屏幕解锁且可以正常使用 */ void onUserPresent(); } }
在一个activity中使用:
package zhangphil.app; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; public class MainActivity extends AppCompatActivity { DeviceScreenListener listener = new DeviceScreenListener(this); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /** * 注册监听 */ listener.register(new DeviceScreenListener.ScreenStateListener() { @Override public void onScreenOn() { Log.d("设备屏幕", "屏幕点亮"); } @Override public void onScreenOff() { Log.d("设备屏幕", "屏幕锁定"); } @Override public void onUserPresent() { Log.d("设备屏幕", "屏幕解锁且可以正常使用"); } }); } @Override protected void onDestroy() { super.onDestroy(); /** * 注销监听 */ listener.unregister(); } }
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/3130.html