ShotWatch: Android Screenshot Detector Library

A few weeks ago, I was asked to show a dialog whenever the user 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.



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

Also published on Medium.

Ilmu jangan disimpan sendiri, bagikan ke teman-teman anda:



Ahmad Fadli Basyari

Mahasiswa IT di President University. Memiliki hobi membuat aplikasi mobile, blogging dan menonton film.

Leave a Reply

Your email address will not be published. Required fields are marked *


[+] kaskus emoticons nartzco