編輯:關於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(); } }); } }
以上所述是小編給大家介紹的Android 監聽鎖屏、解鎖、開屏 功能代碼的相關知識,希望對大家有所幫助!
Handler的定義:主要接受子線程發送的數據, 並用此數據配合主線程更新UI。解釋: 當應用程序啟動時,Android首先會開啟一個主線程 (也就是UI線程) , 主線
應用開發中經常會有從數據庫中讀取數據顯示,然後選中多條、全部記錄並且刪除的需求。在做定制系統聯系人的時候也遇到這樣的需求,下面寫個簡單的通過ListView和CheckB
任何程序都是靜態代碼,我們把這些靜態代碼打包好,然後放到運行環境當中,通過事件流的驅動使這些代碼運行起來。Android的環境也不例外。靜態的代碼,在動態事件的驅動下,才
前言 之前博客裡已經將了MediaPlayer的簡單應用,如何使用MediaPlayer在Android應用中播放音頻。這篇博客在MediaPlayer使用的基礎