1

How can I intercept taking screenshot event on android 4.0+? I want to execute some own code when taking screenshot by vol down+power key.

lseeo
  • 799
  • 2
  • 9
  • 15

2 Answers2

3

Here there is a nice blogpost about preventing screenshot, but I don't know if there is a way to execute code when the user perform a screenshot...

you can use the FLAG_SECURE from the WindowManager in order to avoid screenshots

Example :

public class FlagSecureTestActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().setFlags(LayoutParams.FLAG_SECURE,
                         LayoutParams.FLAG_SECURE);

    setContentView(R.layout.main);
  }
}
nbe_42
  • 1,212
  • 1
  • 14
  • 22
  • 2
    While that link may contain an answer, links break over time. Consider updating your answer with a brief summary, so it is still helpful to others if that link ever goes away :) – Leigh May 13 '13 at 15:15
0

This is not all that much possible, you can eventually listen for changes on the screenshot save location and execute some code using a FileObserver

https://developer.android.com/reference/android/os/FileObserver.html

The biggest problem here is that many devices use different ways of taking screenshots, from not being able to take screenshots at all, to being able to take screenshots without even using any key to take a screenshot.

Using the Flag.Secure suggested in the other answer works in some cases, in others it will mess up your screen.

Aedis
  • 413
  • 3
  • 6
  • can we capture key events? volumn down+power key event. – lseeo May 13 '13 at 14:12
  • Yes, use a KeyListener with a Collection of current pressed keys, see here : http://stackoverflow.com/questions/2623995/swings-keylistener-and-multiple-keys-pressed-at-the-same-time – nbe_42 May 13 '13 at 15:41
  • this is for swing. I'm using android, can android sdk api capture current pressed keys? – lseeo May 13 '13 at 22:53