Android屏幕锁定/解锁以编程方式

时间:2012-10-04 08:24:32

标签: android

我正在开发一个应用程序,我必须以编程方式锁定和解锁屏幕。请帮忙!我不知道如何开发支持每个Android OS版本的此类功能。

2 个答案:

答案 0 :(得分:26)

解锁

KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); 
final KeyguardManager.KeyguardLock kl = km .newKeyguardLock("MyKeyguardLock"); 
kl.disableKeyguard(); 

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
                                 | PowerManager.ACQUIRE_CAUSES_WAKEUP
                                 | PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
wakeLock.acquire();

答案 1 :(得分:14)

此链接可帮助您解决问题:

Unlock and Lock Programmatically

//Get the window from the context    
WindowManager wm = Context.getSystemService(Context.WINDOW_SERVICE);   

//Unlock
Window window = getWindow();  
window.addFlags(wm.LayoutParams.FLAG_DISMISS_KEYGUARD);  

//Lock device  
DevicePolicyManager mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);