您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)Android中怎么監(jiān)聽屏幕是否鎖屏,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
實(shí)現(xiàn)方法:
1)通過BroadcastReceiver接收廣播Intent.ACTION_SCREEN_ON和Intent.ACTION_SCREEN_OFF可以判斷屏幕狀態(tài)是否鎖屏,但是只有屏幕狀態(tài)發(fā)生改變時(shí)才會(huì)發(fā)出廣播;
2)如果要在屏幕狀態(tài)發(fā)生改變之前就想獲取屏幕狀態(tài),可以通過反射機(jī)制調(diào)用PowerManager的isScreenOn方法 。
具體實(shí)現(xiàn),見代碼:
直接上代碼:
1.定義一個(gè)接收廣播的類
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狀態(tài)廣播接收者 */ 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(); } } } /** * 開始監(jiān)聽screen狀態(tài) * * @param listener */ public void begin(ScreenStateListener listener) { mScreenStateListener = listener; registerListener(); getScreenState(); } /** * 獲取screen狀態(tài) */ 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狀態(tài)監(jiān)聽 */ public void unregisterListener() { mContext.unregisterReceiver(mScreenReceiver); } /** * 啟動(dòng)screen狀態(tài)廣播接收器 */ 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 {// 返回給調(diào)用者屏幕狀態(tài)信息 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 , "屏幕關(guān)閉了" , Toast.LENGTH_SHORT ).show(); } @Override public void onUserPresent() { Toast.makeText( MainActivity.this , "解鎖了" , Toast.LENGTH_SHORT ).show(); } }); } }
以上就是Android中怎么監(jiān)聽屏幕是否鎖屏,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。