Android 監聽鎖屏、解鎖、開屏 操作,android鎖屏

來源:互聯網
上載者:User

Android 監聽鎖屏、解鎖、開屏 操作,android鎖屏

1、首先定義 ScreenListener 

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狀態廣播接收者     */    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();            }        }    }    /**     * 開始監聽screen狀態     *     * @param listener     */    public void begin(ScreenStateListener listener) {        mScreenStateListener = listener;        registerListener();        getScreenState();    }    /**     * 擷取screen狀態     */    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狀態監聽     */    public void unregisterListener() {        mContext.unregisterReceiver(mScreenReceiver);    }    /**     * 啟動screen狀態廣播接收器     */    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();           }       });    }}

  

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.