How to block android programmatically

I want to lock the phone when I press the lock button. Someone please help with a simple code.i try with a part of the code from API_Demos, but it shows some error.

0
source share
3 answers

You can lock the Android screen programmatically using the LockScreen class, for example:

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

Take a look at the LockScreen class here .

+1
source

Code:

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

DOES NOT lock the screen. It just locks the keypad lock. When you run

 lock.disableKeyguard(); 

and press the lock button on the device, it will not lock the keypad lock. To lock the screen programmatically, you must go to the "Device Administrator" and use the locknow () method to lock the device.

+1
source

@Bhupinder Please 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); 
0
source

Source: https://habr.com/ru/post/1265676/


All Articles