0

i used the below link to prevent to take screenshot but this will take more time to prevent taking the screenshot after executing the code which is going to prevent the taking of screenshot.

How do I prevent Android taking a screenshot when my app goes to the background?

Code:

if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
}

Phone: galaxy s6 Os: 5.1.1

Community
  • 1
  • 1
user3546693
  • 364
  • 3
  • 18

2 Answers2

0

This code is intended to mark an Activity (or more precise, a Window) as "Secure" not the phone. It only prevents the Activity in which this flag has been set from having its screenshot taken.

To secure an Activity, call this code in the OnCreate. This will not stop screenshots when the Activity is not displayed.

Example:

@Override
protected void onCreate(Bundle b) {
    super.onCreate(b);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);

}

I haven't tried it myself so do not know if it is possible but you could perhaps draw an invisible window on top of the current Activity that has the SECURE flag set. Maybe this would prevent screenshots of the whole phone which I think is what you are asking.

Kuffs
  • 35,581
  • 10
  • 79
  • 92
  • It doesn't work to prevent screenshots whilst in the background but I already stated that. It does work for its intended purpose. – Kuffs Nov 17 '15 at 11:41
0

Just add this line:

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

Before your setContentView() method.

Hasan Jamshaid
  • 1,659
  • 1
  • 11
  • 14