Android螢幕解鎖和點亮

來源:互聯網
上載者:User

有些情境需要程式自動點亮螢幕,解開螢幕鎖,以方便使用者即時操作,下面用代碼來實現這一功能:

  1. //得到鍵盤鎖管理器對象
  2. KeyguardManager km= (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
  3. //參數是LogCat裡用的Tag
  4. KeyguardLock kl = km.newKeyguardLock("unLock");
  5. //解鎖
  6. kl.disableKeyguard();
  7. //擷取電源管理器對象
  8. PowerManager pm=(PowerManager) getSystemService(Context.POWER_SERVICE);
  9. //擷取PowerManager.WakeLock對象,後面的參數|表示同時傳入兩個值,最後的是LogCat裡用的Tag
  10. PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK,"bright");
  11. //點亮螢幕
  12. wl.acquire();
  13. //釋放
  14. wl.release();

需要在AndroidManifest.xml添加許可權:

    <uses-permission android:name="android.permission.WAKE_LOCK" />    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

flags參數說明:

  
 
  1. PARTIAL_WAKE_LOCK: Screen off, keyboard light off
  2. SCREEN_DIM_WAKE_LOCK: screen dim, keyboard light off
  3. SCREEN_BRIGHT_WAKE_LOCK: screen bright, keyboard light off
  4. FULL_WAKE_LOCK: screen bright, keyboard bright

ACQUIRE_CAUSES_WAKEUP:Normal wake locks don't actually turn on the illumination. Instead, they cause the illumination to remain on once it turns on (e.g. from user activity). This flag will force the screen and/or keyboard to turn on immediately,
when the WakeLock is acquired. A typical use would be for notifications which are important for the user to see immediately.

ON_AFTER_RELEASE:f this flag is set, the user activity timer will be reset when the WakeLock is released, causing the illumination to remain on a bit longer. This can be used to reduce flicker if you are cycling between wake lock conditions.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.