Search code examples
androiddevice-admin

Can DevicePolicyManager.lockNow() be called from a non-admin app?


These document the use of lockNow() from an app that's admin.

https://developer.android.com/reference/android/app/admin/DevicePolicyManager.html#lockNow(int)

http://blogs.quovantis.com/android-device-administration-apis/

The basic process is:

  1. create a DeviceAdminReceiver

  2. register the receiver in the manifest, and give it the DEVICE_ADMIN_ENABLED action

  3. declare the security policies in an xml file

  4. initialize a DevicePolicyManager object in your onCreate() method

  5. call DevicePolicyManager.lockNow()

And that works beautifully. However, I'd like to be able to lock the screen without making my app an administrator perhaps by calling lockNow() as an implicit intent. Is this possible?


Solution

  • Only a device admin with the right permissions can call that API.

    Given that, either you must:

    1. Create an app that is a device admin with the USES_POLICY_FORCE_LOCK permission and triggers on an intent of your choosing.
    2. Make your existing app a device admin with the USES_POLICY_FORCE_LOCK permission.
    3. Find an existing app with those requirements. But I am not familiar with any.