android 手機(jī)監(jiān)聽(tīng)屏幕鎖屏,用戶解鎖

用到的場(chǎng)景很多,自己發(fā)揮 ,廢話不多說(shuō)直接上代碼:

private classScreenBroadcastReceiverextendsBroadcastReceiver{

privateStringaction= null;

@Override

public voidonReceive(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();

}

}

}

/**

* 開始監(jiān)聽(tīng)screen狀態(tài)

*

* @paramlistener

*/

public voidbegin(ScreenStateListener listener) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?mScreenStateListener=listener;

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?registerListener();

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?getScreenState();

}

/**

* 獲取screen狀態(tài)

*/

private voidgetScreenState() {

? ? ?PowerManagermanager=(PowerManager)mContext.getSystemService(Context.POWER_SERVICE);

? ? ?if(manager.isScreenOn()) {

? ? ? ? ? ? ? ? if(mScreenStateListener!= null) {

? ? ? ? ? ? ? ? ? ? ? ? ?mScreenStateListener.onScreenOn();

? ? ? ? ? ? ? ?}

? ? ?}else{

? ? ? ? ? ? ? ? if(mScreenStateListener!= null) {

? ? ? ? ? ? ? ? ? ? ? ? mScreenStateListener.onScreenOff();

? ? ? ? ? ? ? ? ?}

? ? ?}

}

/**

* 停止screen狀態(tài)監(jiān)聽(tīng)

*/

public voidunregisterListener() {

? ? ? ? ? ? mContext.unregisterReceiver(mScreenReceiver);

}

/**

* 啟動(dòng)screen狀態(tài)廣播接收器

*/

private voidregisterListener() {

? ? ? ? IntentFilterfilter= newIntentFilter();

? ? ? ? filter.addAction(Intent.ACTION_SCREEN_ON);

? ? ? ? filter.addAction(Intent.ACTION_SCREEN_OFF);

? ? ? ? filter.addAction(Intent.ACTION_USER_PRESENT);

? ? ? ? mContext.registerReceiver(mScreenReceiver,filter);

}

public interfaceScreenStateListener{// 返回給調(diào)用者屏幕狀態(tài)信息

? ? ? ? ? public voidonScreenOn();

? ? ? ? ? public voidonScreenOff();

? ? ? ? ? public voidonUserPresent();

? ? }

}


在上面的代碼中,用到了動(dòng)態(tài)注冊(cè)廣播,在需要監(jiān)聽(tīng)屏幕狀態(tài)的activity中直接調(diào)用,當(dāng)不需要監(jiān)聽(tīng)的時(shí)候,如activity finish的時(shí)候,使用unregisterListener解除廣播監(jiān)聽(tīng);

下面是在Activity中調(diào)用;


ScreenListener screenListener = new ScreenListener(this) ;?

screenListener.begin(new ScreenListener.ScreenStateListener() {

@Override

public void onScreenOn() {

? ? ? ? ? ? ? ? Toast.makeText( CounterTActivity.this , "屏幕打開了" , Toast.LENGTH_SHORT ).show();

}

@Override

public void onScreenOff() {

? ? ? ? ? ? ? ? Toast.makeText( CounterTActivity.this , "屏幕關(guān)閉了" , Toast.LENGTH_SHORT ).show();

}

@Override

public void onUserPresent() {

? ? ? ? ? ? ? ? Toast.makeText( CounterTActivity.this , "解鎖了" , Toast.LENGTH_SHORT ).show();

}

});

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容