3

In my Android app, I need to detect the screen capture event. How do I do this?

eirikdaude
  • 3,106
  • 6
  • 25
  • 50
TOP
  • 2,574
  • 5
  • 35
  • 60
  • *I didn't found the answer for it* *--> it's better if you search for it before finding it. I personally recommend Google's search engine, which I used to find many duplicates of this question without effort (just copy/pasting the title, really). – 2Dee Aug 27 '15 at 08:17
  • @2Dee I tried with Google before creating new question in stack overflow. And in this site I found the same question, but it has no answer. – TOP Aug 27 '15 at 08:20

1 Answers1

0

I have used this code in my application and it works fine. It notifies when a screenshot is taken. I have tested it on OnePlus 1, Nexus 6P, Moto G3. No issues on any devices.

public void detectScreenShotService(final Activity activity) {

    final Handler h = new Handler();
    final int delay = 3000; //milliseconds
    final ActivityManager am = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);

    h.postDelayed(new Runnable() {
        public void run() {

            List<ActivityManager.RunningServiceInfo> rs = am.getRunningServices(200);

            for (ActivityManager.RunningServiceInfo ar : rs) {
                if (ar.process.equals("com.android.systemui:screenshot")) {
                    Toast.makeText(activity, "Screenshot captured!!", Toast.LENGTH_LONG).show();
                }
            }
            h.postDelayed(this, delay);
        }
    }, delay);
}
Akshay Bhat 'AB'
  • 2,690
  • 3
  • 20
  • 32
Sumit Pansuriya
  • 543
  • 10
  • 23
  • It can affect performance, because all actions are executed on Main Thread. To avoid it use something like HandlerThread for executing on background (http://stackoverflow.com/a/29389627/5222184) – qpator Jan 12 '17 at 18:05
  • According to [this SO answer](https://stackoverflow.com/a/40218889/4619820), the code only works about 1/4 of the time and generates multiple calls for the same event. – Abandoned Cart Feb 29 '20 at 00:33