Azhar Ali Azhar Ali - 1 month ago 33
iOS Question

Xamarin iOS/Android: Detect Lockscreen /home screen click detection or swipe detection

I am working on a mobile app using xamarin forms, is it possible to detect swipe gesture in android for screenshot or click of lock screen or home screen ?

If it is possible via objective C/swift/java then it would also be possible through Xamarin.

Answer

Android

I do not believe you can setup a swipe gesture / click during the Lock Screen as it's a separate Activity in a different process(System app). You also can't replace the lock screen with a user application.

You can control some of this via the Device Admin API:

https://developer.android.com/guide/topics/admin/device-admin.html#lock

However that's only for the opposite of what you want(Probably to know if the user finished the lock screen).

You can instead listen for the ACTION_USER_PRESENT or the fairly new ACTION_USER_UNLOCKED broadcast via a Broadcast Receiver:

https://developer.android.com/reference/android/content/Intent.html#ACTION_USER_PRESENT (API 3)

https://developer.android.com/reference/android/content/Intent.html#ACTION_USER_UNLOCKED (API 24)

https://developer.android.com/training/articles/direct-boot.html#notification

iOS

You can use Darwin Notifications - https://developer.apple.com/reference/corefoundation/1666719-cfnotificationcenter

You can check the following events:

  • com.apple.springboard.hasBlankedScreen - When the screen goes blank (One of a few times it will)
  • com.apple.springboard.lockstate - What state the lock is in(locked/unlocked)
  • com.apple.springboard.lockcomplete - The device locked

Is there a away to detect the event when iOS device goes to sleep mode (when the screen gets blackened)?

Lock Unlock events iphone