android唤醒屏幕并解锁
生活随笔
收集整理的這篇文章主要介紹了
android唤醒屏幕并解锁
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在開(kāi)發(fā)程序的時(shí)候,有時(shí)會(huì)有喚醒屏幕并解鎖屏幕的需求。
這個(gè)demo的作用是在進(jìn)入主界面后30秒喚醒屏幕并解鎖,僅供演示喚醒屏幕功能。
首先加入以下兩個(gè)權(quán)限:
<uses-permission android:name="android.permission.WAKE_LOCK" /><uses-permission android:name="android.permission.DISABLE_KEYGUARD" />然后是activity中的代碼,喚醒并解鎖屏幕
/** 代碼喚醒屏幕demo* 程序執(zhí)行后一段時(shí)間喚醒屏幕*/import android.app.Activity; import android.app.KeyguardManager; import android.app.KeyguardManager.KeyguardLock; import android.content.Context; import android.os.Bundle; import android.os.PowerManager; import android.util.Log; import android.view.Menu;public class MainActivity extends Activity {Context context = MainActivity.this;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Thread wakeUpThread = new Thread(new WakeUpThread());wakeUpThread.start();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}class WakeUpThread implements Runnable{@Overridepublic void run() {// TODO Auto-generated method stubtry {Thread.sleep(30*1000);//休眠一分鐘//獲取電源管理器對(duì)象Log.e("zhiyinqing","該開(kāi)啟屏幕了");PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.SCREEN_DIM_WAKE_LOCK,"bright");wl.acquire(); //點(diǎn)亮屏幕//wl.release();//釋放//屏幕解鎖KeyguardManager km= (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);//得到鍵盤鎖管理器對(duì)象KeyguardLock kl = km.newKeyguardLock("unLock");//參數(shù)是LogCat里用的Tagkl.disableKeyguard();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}總結(jié)
以上是生活随笔為你收集整理的android唤醒屏幕并解锁的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: php中单引号与双引号的区别
- 下一篇: android插上耳机仍用扬声器播放音频