ShotWatch: An Android Screenshot Detector Library

A few weeks ago, I was asked to show a dialog whenever users took a screenshot in certain pages. The aim is to make it easier for users to send the picture to other apps, without having to go to the gallery to find it. It also allows them to share it as deep-link to that page instead of the picture that was taken.

Snapchat has implemented something similar, whenever we took a screenshot someone else post, that someone else will be notified. So I thought it would be pretty easy. Unfortunately, unlike iOS, there is no direct API in Android for this. So I had to do some research on the available workarounds.

A little googling brings me up to an SOF link which the top answer is suggesting to use a FileObserver or ContentObserver. The other person that answer to that question stated that FileObserver would not work in Android M (haven’t tested it yet) so I went with ContentObserver.

Since there is no library referred in those answers, I decided to create one: ShotWatch.

The usage is fairly simple. You just need to provide it with ContentResolver and a Listener when a screenshot has been taken.

You can decide on when to register and unregister the listener. As for my case, I only need to listen whenever user take a screenshot of a page within the app so onPause() and onResume() would do the job.

ShotWatch in Action

Android M

Be careful when targeting Android M since you also need to handle runtime permission. I would recommend you to take a look at Dexter or PermissionUtil.

See all the code in GitHub

Originally published at AbangFadli Blog.