Android點亮屏幕和解鎖完整示例

jopen 10年前發布 | 108K 次閱讀 Android Android開發 移動開發

MainActivity如下:

    package cc.test.testwakelock;  

    import android.os.Bundle;  
    import android.app.Activity;  
    import android.content.Context;  
    import android.content.Intent;  
    /** 
     * Demo描述: 
     * 點亮屏幕和解開鍵盤鎖完整示例 
     *  
     * 注意權限: 
     * <uses-permission android:name="android.permission.WAKE_LOCK"/> 
     * <uses-permission android:name="android.permission.DEVICE_POWER"/> 
     * <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/> 
     *  
     * 測試方法: 
     * 程序運行后,里面按下電源鍵即可. 
     *  
     * 參考資料: 
     * 1 http://code.eoe.cn/399/title/%E4%BD%BF%E7%94%A8WakeLock%E4%BD%BFAndroid%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F%E4%BF%9D%E6%8C%81%E5%90%8E%E5%8F%B0%E5%94%A4%E9%86%92 
     * 2 http://bbs.csdn.net/topics/360073862 
     * 3 http://www.cnblogs.com/keyindex/articles/1819504.html 
     * 4 http://www.cnblogs.com/stoic/archive/2011/11/01/2785220.html 
     *   Thank you very much 
     */  
    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如下:

    package cc.test.testwakelock;  

    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;  
    /** 
     * 注意方法: 
     * WakeLock android.os.PowerManager.newWakeLock(int levelAndFlags, String tag) 
     * 該方法第一個參數為flag,有幾個不同的變量.請查閱官方API 
     * 具體的需求,視情況而定 
     * 
     */  
    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();  
            }  
        }  

    }  

main.xml如下:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        xmlns:tools="http://schemas.android.com/tools"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        >  

        <TextView  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="利用WakeLock點亮屏幕"  
            android:layout_centerInParent="true" />  

    </RelativeLayout>  


AndroidManifest.xml如下:

<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    package="cc.test.testwakelock"  
    android:versionCode="1"  
    android:versionName="1.0"  
    >  

    <uses-sdk  
        android:minSdkVersion="10"  
        android:targetSdkVersion="17" />  

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


    <application  
        android:allowBackup="true"  
        android:icon="@drawable/ic_launcher"  
        android:label="@string/app_name"  
        android:theme="@style/AppTheme" >  
        <activity  
            android:name="cc.test.testwakelock.MainActivity"  
            android:label="@string/app_name" >  
            <intent-filter>  
                <action android:name="android.intent.action.MAIN" />  

                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>  
        </activity>  

        <service android:name=".ScreenAndLockService"/>  
    </application>  

</manifest>  
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!