如何以编程方式锁定android

时间:2010-12-15 06:27:38

标签: android locking

当我点击锁定按钮时,我想锁定手机。任何人请帮助使用简单的代码。我尝试使用API​​_Demos中的部分代码,但它显示了一些错误。

3 个答案:

答案 0 :(得分:1)

您可以使用LockScreen类以编程方式锁定Android屏幕,如下所示:

KeyguardManager mgr = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE); 
KeyguardLock lock = mgr.newKeyguardLock(KEYGUARD_SERVICE); 
lock.reenableKeyguard();

查看LockScreen类here

答案 1 :(得分:1)

代码:

KeyguardManager mgr = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE); 
KeyguardLock lock = mgr.newKeyguardLock(KEYGUARD_SERVICE); 
lock.reenableKeyguard();

不会锁定屏幕。它只启用键盘锁。当您使用

运行时
lock.disableKeyguard();

并按下设备上的锁定按钮,它不会锁定键盘锁。 要以编程方式锁定屏幕,您必须引用Device Admin并使用locknow()方法锁定设备。

答案 2 :(得分:0)

@Bhupinder请检查以下链接。

http://musicm122.blogspot.in/2011/10/locking-and-unlocking-android-phone.html

//Get the window from the context

   WindowManager wm = Context.getSystemService(Context.WINDOW_SERVICE);

   //Unlock

   //http://developer.android.com/reference/android/app/Activity.html#getWindow()

   Window window = getWindow();  

   window.addFlags(wm.LayoutParams.FLAG_DISMISS_KEYGUARD);  



   //Lock device

   DevicePolicyManager mDPM;

   mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);