1. 程式人生 > >Android-點亮螢幕與喚醒

Android-點亮螢幕與喚醒

實現點亮螢幕和解開鍵盤鎖,核心程式碼如下。

注意在AndroidManifest.xml中新增以下許可權

<uses-permission android:name="android.permission.WAKE_LOCK"/> 
<uses-permission android:name="android.permission.DEVICE_POWER"/> 
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/> 
 import android.os.Bundle;  
 import
android.app.Activity; import android.content.Context; import android.content.Intent; public class MainActivity extends Activity { private Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private
void init() { mContext = this; for (int i = 0; i < 20000; i++) { for (int j = 0; j < 20000; j++) { if (i == 19999 && j == 19999) { Intent intent = new Intent(mContext,ScreenAndLockService.class); startService(intent); } } } } //終止服務
@Override public void onBackPressed() { super.onBackPressed(); Intent intent = new Intent(mContext,ScreenAndLockService.class); stopService(intent); } //終止服務 @Override protected void onDestroy() { super.onDestroy(); Intent intent = new Intent(mContext,ScreenAndLockService.class); stopService(intent); } }

ScreenAndLockService.java如下:

    import android.app.KeyguardManager;  
    import android.app.KeyguardManager.KeyguardLock;  
    import android.app.Service;  
    import android.content.Context;  
    import android.content.Intent;  
    import android.os.IBinder;  
    import android.os.PowerManager;  

    public class ScreenAndLockService extends Service {  
        // 鍵盤管理器  
        KeyguardManager mKeyguardManager;  
        // 鍵盤鎖  
        private KeyguardLock mKeyguardLock;  
        // 電源管理器  
        private PowerManager mPowerManager;  
        // 喚醒鎖  
        private PowerManager.WakeLock mWakeLock;  

        @Override  
        public IBinder onBind(Intent arg0) {  
            return null;  
        }  

        @Override  
        public void onCreate() {  
            super.onCreate();  
            System.out.println("----> 開啟服務");  
            mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);  
            mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);  

        }  

        @Override  
        public void onStart(Intent intent, int startId) {  
            // 點亮亮屏  
            mWakeLock = mPowerManager.newWakeLock  
            (PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "Tag");  
            mWakeLock.acquire();  
            // 初始化鍵盤鎖  
            mKeyguardLock = mKeyguardManager.newKeyguardLock("");  
            // 鍵盤解鎖  
            mKeyguardLock.disableKeyguard();  
        }  

        //一定要釋放喚醒鎖和恢復鍵盤  
        @Override  
        public void onDestroy() {  
            super.onDestroy();  
            if (mWakeLock != null) {  
                System.out.println("----> 終止服務,釋放喚醒鎖");  
                mWakeLock.release();  
                mWakeLock = null;  
            }  
            if (mKeyguardLock!=null) {  
                System.out.println("----> 終止服務,重新鎖鍵盤");  
                mKeyguardLock.reenableKeyguard();  
            }  
        }  

    }