相关文章推荐
重感情的围巾  ·  史上最简单node ...·  1 年前    · 
爱笑的高山  ·  js ...·  1 年前    · 
爽快的绿豆  ·  Web service error ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

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.

@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. – CommonsWare Mar 22, 2012 at 13:22 @StingRay5: FLAG_SECURE has existed since the beginning -- it just had limited meaning until recently. – CommonsWare Mar 22, 2012 at 14:19 Interesting to note that there is a method called onCreateThumbnail ni the activity lifecycle, but it's never used. – Snicolas Aug 8, 2012 at 5:11 @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. – CommonsWare Jun 17, 2013 at 10:24 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. – Shobhit Puri Aug 25, 2013 at 11:16

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:

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

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 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 Oct 29, 2012 at 12:13 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 Jun 23, 2014 at 8:43

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.

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 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 Oct 17, 2015 at 10:02 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. – user1064249 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 – Alexiscanny Jan 4, 2018 at 18:11

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);
                Exactly what I was looking for, thanks! One can also call addFlags() instead of setFlags(), which makes it even easier.
– martinstoeckli
                Nov 29, 2021 at 10:59

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

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.

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

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.