169

The app I'm currently building has the requirement that the app has to prevent the OS to take a screenshot of the app when it's being pushed into the background for security reasons. This way it won't be able to see the last active screen when switching between apps.

I'm planning to put this functionality in the application class's onPause method, but first I need to find out how I can achieve this functionality.

So is there anybody out there, that has a clue how to fix this?

8 Answers 8

304

Try FLAG_SECURE:

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

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

    setContentView(R.layout.main);
  }
}

This definitely secures against manual screenshots and automatic screenshots from the ICS recent-tasks history. It also secures against screen recording (e.g., apps using the media projection APIs).

UPDATE: it also secures against Now On Tap or other assistants on Android 6.0; they will not get access to the details of widgets and containers in your UI if the user brings up the assistant.

UPDATE #2: however, not everything in the activity will be protected. Any pop-up windows — Dialog, Spinner, AutoCompleteTextView, action bar overflow, etc. — will be insecure. You can fix the Dialog problem by calling getWindow() on it and setting FLAG_SECURE. The rest... gets tricky. See this blog post for more.

33
  • 6
    @Dany's: Actually, it has everything to do with the question. I just tested it on a Nexus S (4.0.3), and it works as expected -- the recent-tasks screenshot is suppressed. You still see the application name and icon, but the screenshot is black. You are welcome to try it on ICS hardware. Commented Mar 22, 2012 at 13:22
  • 2
    @StingRay5: FLAG_SECURE has existed since the beginning -- it just had limited meaning until recently. Commented Mar 22, 2012 at 14:19
  • 1
    Interesting to note that there is a method called onCreateThumbnail ni the activity lifecycle, but it's never used.
    – Snicolas
    Commented Aug 8, 2012 at 5:11
  • 2
    @PankajKumar: If the "code to take screen shot" that you wrote is that classic "grab the framebuffer as root" trick, FLAG_SECURE won't defend against that, as that's too low-level. Similarly, if you took your own widget hierarchy and had it draw to a Bitmap-backed Canvas, FLAG_SECURE probably won't defend against that, as you are taking your own screenshots of your own widgets and therefore presumably wanted the "secure" ones in there. FLAG_SECURE is for system screenshots, such as the recent-tasks thumbnails. Commented Jun 17, 2013 at 10:24
  • 2
    Wonderful and simple answer! I'm wondering why apps like snapchat don't use this to prevent someone taking a screenshot of the images sent. Commented Aug 25, 2013 at 11:16
81

Be careful about using WindowManager.LayoutParams.FLAG_SECURE, on some devices (verified on Samsung Galaxy ACE, e.g. GT-S5830) this will make the view scrambled. Looks like a Samsung specific bug. I recommend the following:

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

This is what a scrambled screen looks like:

Scrambled Screen Image

This is working properly on ICS Samsung phones though, so I'm assuming problem is isolated to Gingerbread devices (or older).

7
  • 9
    You're totally right, we came across this issue, and it certainly was needed to check for the API level. Actually forgot to post this, so thanks for bringing it up again. :)
    – StingRay5
    Commented Jun 27, 2012 at 18:54
  • It also occurs on Samsung Gio (Gingerbread) but on Froyo its ok - so it occurs only on 2.3
    – pixel
    Commented Oct 29, 2012 at 12:13
  • We noticed this problem as well. You can verify in the Emulator on Gingerbread devices. Commented Dec 31, 2012 at 19:40
  • I can also confirm that this problem appears on Samsung Gingerbread devices. Commented Apr 17, 2013 at 7:44
  • 7
    Even on newer Android versions FLAG_SECURE can be problematic: on Android 4.3 FLAG_SECURE causes animation problems in screen rotation animation, see code.google.com/p/android/issues/detail?id=58632 - this has been fixed on Android 4.4
    – Oliver
    Commented Jun 23, 2014 at 8:43
13

The solution provided by CommonsWare continues to be valid also in Lollipop.

Just a note, if you want to continue to not see snapshots in recent list for the entire app, ALL the implemented activities should specify in the onCreate() method the flag getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); before setContentView();

Otherwise a snapshot in the recent list will show the first activity without the flag if the user navigated through it.

4
  • 1
    In my app, I have functionality of setting/clearing FLAG_SECURE based on a user preference. It worked fine till KITKAT and black screen can be seen in task switcher. In Lollipop, the Overview displays the last snapshot of my app, the time when FLAG_SECURE was not set(cleared).
    – r.bhardwaj
    Commented Apr 10, 2015 at 12:27
  • @r.bhardwaj i think we can't do so much in that case, but you can advice the user that, the change needs App to be restarted to take fully effect ;-) and you are done.
    – Davideas
    Commented Oct 17, 2015 at 10:02
  • 1
    Anyone found a way to inflate a custom view before snapshot is taken so it's not just a black screen? I've tried inflating a view on window manager in onPause, but it seems by then it's too late. Commented Feb 2, 2017 at 14:39
  • Hi @user1064249 Have you found any solution? I want inflate the Launcher View before the OS take the screenshot Commented Jan 4, 2018 at 18:11
6

In case if someone is looking for a solution in which the app must secure (screen overlay) when the app is background or stick of all running app and the in-app app should allow screenshot. Try Below:-

@Override
    protected void onResume() {
        super.onResume();
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
    }

    @Override
    protected void onPause() {
        super.onPause();
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
    }
1
  • 1
    Exactly what I was looking for, thanks! One can also call addFlags() instead of setFlags(), which makes it even easier. Commented Nov 29, 2021 at 10:59
5

As of Android 13, there is a new way to prevent a screenshot being taken for the recent apps list, while still allowing the user to take screenshots while using the app. In your main activity, call setRecentsScreenshotEnabled(false).

3

Here is a solution for hiding content of an app by covering it with a splash screen when the app is put into the background. This is not using the FLAG_SECURE technique, I simply override the onPause and onResume methods of the screens and modify the view to show one that covers everything in the back.

https://stackoverflow.com/a/52976001/6686912

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

this worked for me, it prevents from taking screenshot and also any inbuilt or third party recording application from recording screen.

1

This is work for me after adding these line into the onCreate before setContentView of every activity.

getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,     
WindowManager.LayoutParams.FLAG_SECURE);
setContentView(R.layout.activity_notification);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.