編輯:關於 Android 程式設計
要實作自動解鎖並點亮螢幕的功能,需要同時用到 KeyguardManager(鍵盤鎖管理器)與 PowerManager(電源管理器)。以下說明完整的做法。
// 取得鍵盤鎖管理器物件
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
KeyguardLock kl = km.newKeyguardLock("unLock"); // 參數為 LogCat 中使用的 Tag
kl.disableKeyguard(); // 解除鎖定
// 取得電源管理器物件
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
// 取得 WakeLock 物件,後面的參數以 | 同時傳入兩個值,最後是 LogCat 的 Tag
PowerManager.WakeLock wl = pm.newWakeLock(
PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "bright");
wl.acquire(); // 點亮螢幕
wl.release(); // 釋放資源
要讓上述功能正常運作,必須在 AndroidManifest.xml 中加上以下權限:
<uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
透過 KeyguardLock 的 disableKeyguard() 解除鎖屏,再搭配 WakeLock 的 acquire() 喚醒螢幕,即可達成自動解鎖並點亮螢幕的效果。
Material Design 中的 RecyclerView 與 CardView:列表與卡片這樣做
Android 5.0 帶來兩個外觀與動畫都遵循 Material 規範的新元件:RecyclerView 是更模組化、效能更好的列表容器,支援多種版面型態;CardVi
Android 02:Started Service — 被啟動的服務
1. Started Service 介紹 Started Service,即「被啟動的服務」,是兩種常見服務之一,另一種是 Bound Service。它常用來執行某個
AT Commands 修煉手記(三):把 ATCoP 從裡到外拆一遍
這是「AT Commands:從白癡到大師的修煉歷程」系列的第三篇。前面鋪墊完基礎之後,這次要動真格的,把高通平台裡真正幹活的那個模塊——ATCommandProcess
Cordova 5.3.1(PhoneGap)混合開發環境搭建完整指南
引言:認識 Cordova先簡單介紹 Cordova 的來歷。Cordova 的前身叫 PhoneGap,自被 Adobe 收購後交由 Apache 管理,並將核心功能開