23

我正在开发一个应用程序,我必须在其中以编程方式锁定和解锁屏幕。请帮忙!我不知道如何开发这种支持每个版本的 Android 操作系统的功能。

4

2 回答 2

28

开锁

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();
于 2013-11-07T13:08:52.137 回答
12

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

以编程方式解锁和锁定

//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);
于 2012-10-04T08:28:49.177 回答