編輯:關於Android編程
(1) 監聽BroadcastReceiver
(2)獲取PowerManager事件
總共有3個事件:
Intent.ACTION_SCREEN_ON : 屏幕點亮 Intent.ACTION_SCREEN_OFF :屏幕關閉 Intent.ACTION_USER_PRESENT: 用戶解鎖
監聽用戶解鎖需要在AndroidManifest中注冊權限
if (mContext != null) { 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); }
PowerManager manager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); if (manager.isScreenOn()) { if (mScreenStateListener != null) { mScreenStateListener.onScreenOn(); } } else { if (mScreenStateListener != null) { mScreenStateListener.onScreenOff(); } }
import android.annotation.SuppressLint; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.PowerManager; /** * Created by xiyanglu on 4/11/15. */ public class ScreenObserver { private Context mContext; private ScreenBroadcastReceiver mScreenReceiver; private ScreenStateListener mScreenStateListener; public ScreenObserver(Context context) { mContext = context; mScreenReceiver = new ScreenBroadcastReceiver(); } public void startObserver(ScreenStateListener listener) { mScreenStateListener = listener; registerListener(); getScreenState(); } public void shutdownObserver() { unregisterListener(); } /** * 獲取screen狀態 */ @SuppressLint("NewApi") private void getScreenState() { if (mContext == null) { return; } PowerManager manager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); if (manager.isScreenOn()) { if (mScreenStateListener != null) { mScreenStateListener.onScreenOn(); } } else { if (mScreenStateListener != null) { mScreenStateListener.onScreenOff(); } } } private void registerListener() { if (mContext != null) { 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); } } private void unregisterListener() { if (mContext != null) mContext.unregisterReceiver(mScreenReceiver); } 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(); } } } public interface ScreenStateListener {// 返回給調用者屏幕狀態信息 public void onScreenOn(); public void onScreenOff(); public void onUserPresent(); } }
[1] Android 監聽屏幕解鎖事件 http://blog.csdn.net/gf771115/article/details/7991892
一、前言在Android客戶端開發中,使用網絡請求是非常常見的事情,一般我們使用HttpURLConnection是可以滿足需求的,不過隨著業務邏輯復雜,依然還是有很多不
在移動應用中,很多時候都會用到圖片選擇、圖片裁剪等功能。最近我也在准備一個開源的相冊項目,以方便以後開發應用的時候使用,也盡可能的方便需要的人。一個完整的相冊,應該包含相
設計過一款基於開源的XMPP即時通信協議的軟件,采用C/S協議,通過GPRS無線網絡用TCP協議到服務器,以架設開源的Openfire 服務器作為即時通訊平台 系統主要由
本文實例分析了Android編程中activity的完整生命周期。分享給大家供大家參考,具體如下:android中 activity有自己的生命周期,對這些知識的學習可以