Android点亮屏幕和解锁完整示例
jopen
11年前
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>