how to lock the android programatically

非 Y 不嫁゛ 提交于 2019-12-19 11:57:09

问题


i want to lock the phone when i click the lock button.anybody please help with simple code.i tried with part of code from API_Demos but it shows some error.


回答1:


You can lock the Android's screen programmatically using the LockScreen class like so:

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

Take a look at the LockScreen class here.




回答2:


The code:

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

will NOT lock the screen. It just enables the keyguard lock. When you run with

lock.disableKeyguard();

and press lock button on the device it will not lock the keyguard. To lock the screen programatically you have to refer to Device Admin and use locknow() method to lock the device.




回答3:


@Bhupinder Kindly check the following link.

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);


来源:https://stackoverflow.com/questions/4447312/how-to-lock-the-android-programatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!