3

I'm using device-admin policy to lock(lockNow),but how can I lock repeatedly for specific amount of time.In the mentioned time interval the user should not be able to unlock the phone!

Abdul Khader
  • 79
  • 1
  • 7

1 Answers1

2

There is no official API available to achieve your intended behavior. I would strongly recommend to not obstruct the user's actions. But if you want to proceed, you can try following workaround:

  1. When lockNow is triggered, the screen is turned OFF. You can register for SCREEN_ON BroadcastReceiver in a foreground service and then whenever your screen is turned on you can call lockNow. Stop the service and release the broadcast receiver once your timeout has been achieved. The drawback is bad user experience.
  2. You can create a Overlay layout which with Black background and cover user's screen. You can also present some timer to indicate remaining time. Once timeout is achieved, remove the overlay. This will provide better user experience. You might need to take care of overlay permission in this case.
Sagar
  • 23,903
  • 4
  • 62
  • 62
  • I have used a while loop that will run till the specified time and it will be locking the screen continuously.Will that be ok ? – Abdul Khader Apr 17 '18 at 10:27
  • 1
    It would be battery consuming. And the user experience wont be great. User could easily think that your app is causing some issue and would try to uninstall it. I would recommend the overlay approach which will block the UI and you can display meaningful text/graphics to user. This will make your app look more responsive. – Sagar Apr 17 '18 at 10:35