How can I detect if screen lock is set (not whether screen is currently locked)?

414 views
Skip to first unread message

Paddy Landau

unread,
May 20, 2016, 3:57:36 AM5/20/16
to Tasker
I can find threads on how to detect whether the screen is locked or unlocked. However, that's not quite what I want.

Instead, I want to detect if a screen lock is set in the Security settings — it doesn't matter what type of setting.

So:
  • Return true if a Pattern, PIN or Password is set; false if None or Swipe is set.
  • It doesn't matter whether the phone is currently on or off, or whether it is currently locked or unlocked.
The reason why I want this:
  • I use Smart Unlock Universal, which clears the lock while I'm at home and sets it again when I leave. (This is like Google's Smart Lock, but reliable.)
  • However, I also use Android Pay. If I accidentally start it while the lock is cleared, Android Pay automatically removes my card and I have to go through the process to add the card again (a sensible security requirement from my bank). I must start Android Pay only while my lock is set.
  • So, I want to write a Tasker "starter" app that will start Android Pay only if the lock is set (regardless of whether the phone is currently locked or unlocked), and give an error message if the lock is unset. I will use this Tasker app to start Android Pay.
Thank you.

Paddy Landau

unread,
May 20, 2016, 4:37:46 AM5/20/16
to Tasker
After some searching, I've been looking at the KeyguardManager.isDeviceSecure Java function, but unfortunately I am in over my head. I cannot make this work.

Sean Williams

unread,
May 20, 2016, 4:00:26 PM5/20/16
to Tasker
Hi Paddy,


Java - Is Secure (463)
A1: Java Function [ Return:(KeyguardManager)kgm Class Or Object:CONTEXT Function:getSystemService
{Object} (String) Param:keyguard Param: Param: Param: Param: Param: Param: ]
A2: Java Function [ Return:%keyguard Class Or Object:(KeyguardManager)kgm Function:isKeyguardSecure
{boolean} () Param: Param: Param: Param: Param: Param: Param: ]
A3: Flash [ Text:%keyguard Long:Off ]

Paddy Landau

unread,
May 20, 2016, 4:39:46 PM5/20/16
to Tasker
Sean, that's brilliant. The only part that I got right by myself was "isKeyguardSecure {boolean} ()", LOL. Everything else was wrong.

Thank you so much for your help.

Sean Williams

unread,
May 20, 2016, 4:47:56 PM5/20/16
to Tasker
Thank you so much for your help.


Nice use case, formatted question and you also done the research.

I was more than happy to help.

Sean Williams

unread,
May 20, 2016, 5:09:39 PM5/20/16
to Tasker
isKeyguardSecure {boolean}

FYI - use isDeviceSecure() if you don't want to detect SIM locked states.

Sean Williams

unread,
May 20, 2016, 5:10:48 PM5/20/16
to Tasker
isDeviceSecure() - API 23+

Paddy Landau

unread,
May 21, 2016, 5:02:49 AM5/21/16
to Tasker
Thanks for the extra info, Sean.
Reply all
Reply to author
Forward
0 new messages