lseeo
lseeo

Reputation: 839

How to intercept android take screenshot

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.

Upvotes: 1

Views: 3780

Answers (2)

Aedis
Aedis

Reputation: 423

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.

Upvotes: 0

nbe_42
nbe_42

Reputation: 1222

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);
  }
}

Upvotes: 3

Related Questions