Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android 監聽鎖屏、解鎖、開屏 操作,android鎖屏

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

編輯:關於android開發

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();
           }
       });

    }
}

  

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved