Lock screen programmatically on Android

I need to lock the screen programmatically from my application.

I used the following code:

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

I set the following permission in AndroidManifest.

  <uses-permission android:name="android.permission.DISABLE_KEYGUARD" /> 

However, this does not give me any result and does not even give me any errors or exceptions.

What am I missing?

+4
source share
1 answer

If your minimum supported OS is 2.2, you can use the DevicePolicyManager lockNow () method.

If you want to know more about the DevicePolicyManager interface, try taking a look at DeviceAdminSample in the ApiDemos sample code. The Android SDK comes with all the samples, so you can easily add ApiDemos as an Eclipse project and run it in a simulator.

If you need to also support older versions of the OS, see my question here:
Using the OSK DevicePolicyManager SDK classes on Android that support OS 2.1 devices

+5
source

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


All Articles