Android開屏、鎖屏、解鎖監聽實現

ChicoChen發表於2018-07-04
實現方式

監聽BroadcastReceiver

事件
  • Intent.ACTION_SCREEN_ON
  • Intent.ACTION_SCREEN_OFF
  • Intent.ACTION_USER_PRESENT
許可權

監聽使用者解鎖需要在AndroidManifest中註冊許可權

<uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />
複製程式碼
具體實現
  • 1、建立監聽介面
public interface ScreenStatusListener {
    
    void onScreenOn();

    void onScreenOff();
    
    void userPresent()
}
複製程式碼

實現三個方法:開屏、鎖屏、解鎖

  • 2、建立控制工具類
public class ScreenStatusController {

    private Context mContext;
    private IntentFilter mScreenStatusFilter = null;
    private ScreenStatusListener mScreenStatusListener = null;

    public ScreenStatusController(Context context) {
        mContext = context;
        mScreenStatusFilter = new IntentFilter();
        mScreenStatusFilter.addAction(Intent.ACTION_SCREEN_ON);
        mScreenStatusFilter.addAction(Intent.ACTION_SCREEN_OFF);
        mScreenStatusFilter.addAction(Intent.ACTION_USER_PRESENT);
    }
    
    private BroadcastReceiver mScreenStatusReceiver = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Intent.ACTION_SCREEN_ON.equals(action)) { // 開屏
                if (mScreenStatusListener != null) {
                    mScreenStatusListener.onScreenOn();
                }
            } else if (Intent.ACTION_SCREEN_OFF.equals(action)) { // 鎖屏
                if (mScreenStatusListener != null) {
                    mScreenStatusListener.onScreenOff();
                }
            } else if (Intent.ACTION_USER_PRESENT.equals(action)) { //解鎖
                if (mScreenStatusListener != null) {
                    mScreenStatusListener.userPresent();
                }
            }
        }
    };
    
    //設定監聽
    public void setScreenStatusListener(ScreenStatusListener l) {
        mScreenStatusListener = l;
    }
    
    
     //開始監聽
    public void startListen() {
        if (mContext != null) {
            mContext.registerReceiver(mScreenStatusReceiver, mScreenStatusFilter);
        }
    }

    //結束監聽
    public void stopListen() {
        if (mContext != null) {
            mContext.unregisterReceiver(mScreenStatusReceiver);
        }
    }
}
複製程式碼

控制工具主要是實現了廣播的註冊和回收,並處理廣播事件

  • 3、在指定位置實現
public class VideoActivity extends Activity{
    private ScreenStatusController mScreenStatusController = null;
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        //TODO
        
        mScreenStatusController = new ScreenStatusController(this);
        mScreenStatusController.setScreenStatusListener(new ScreenStatusController.ScreenStatusListener() {
            @Override
            public void onScreenOn() {
            }

            @Override
            public void onScreenOff() {

            }
            
            @Override
            pubic void userPresent(){
                
            }
        });
        mScreenStatusController.startListen();
        
    }
    
    @Override
    protected void onDestroy() {
    
        mScreenStatusController.stopListen();
        super.onDestroy();
        
    }
}
複製程式碼

在Video主要是例項化控制工具類,並設定監聽,同時在onCreate和onDestroy中註冊和銷燬廣播事件。

到此為止,開屏、鎖屏、解鎖的監聽就實現完了。

相關文章