25

I mainly want to blank the screen in the recent apps list due to sensitive data being shown. For this, the solution is to use:

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

but this also disallows screenshots, which is a problem.

Is there a way to show a blank screen (or a predefined image) in the recent apps list, while still allowing screenshots?

Rajath
  • 11,787
  • 7
  • 48
  • 62
  • 1
    I am not sure if this will work and I don't have a test environment available right now, but you might try making your sensitive fields invisible in `onPause` and restoring them `onResume` – Robbe Roels Apr 15 '15 at 06:46
  • @RobbeRoels, that sounds good - I'll give that a try – Rajath Apr 15 '15 at 07:15
  • @rajath did you find a solution for this that works not only for Android 8+, but for Android 6 and 7 as well? – Helen Hakobyan Jan 08 '20 at 12:47
  • Does this answer your question? [How do I prevent Android taking a screenshot when my app goes to the background?](https://stackoverflow.com/questions/9822076/how-do-i-prevent-android-taking-a-screenshot-when-my-app-goes-to-the-background) – Lee Kang Oct 10 '22 at 19:38

4 Answers4

21

I have tried this method and it's working. For making the screen blank in recent list and still allowing screen you can set the flag in onPause() and clear the flag in onResume().

@Override
protected void onPause() {
  super.onPause();
  getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,WindowManager.LayoutParams.FLAG_SECURE);
}

@Override
protected void onResume() {
    super.onResume();
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);     
}
Alexander Pacha
  • 9,187
  • 3
  • 68
  • 108
Vrushab Maitri
  • 330
  • 3
  • 14
  • This solution doesn't seem to work on Samsung devices. Any workarounds? – Surekha Jan 16 '19 at 16:45
  • 2
    Works great on Android 8,9,10 But for previous versions is not working. – Jaimoto Aug 26 '19 at 03:12
  • This doesn't seem to work on Pixel 3 and 4 on Android OS version 10. But works fine on Samsung running Android 10 (: . – Surekha Mar 17 '20 at 18:45
  • 1
    I tested on Huawei and Samsung devices, both with Android 10. On both devices this solution does not work unfortunately. Thumbnail in the recent apps list just stays like without the flag. – Benjamin Menrad Sep 25 '20 at 13:03
1

I think what you need is:

android:excludeFromRecents="true"

put that in your android manifest inside your <activity>(activity you want to exclude from recents)</activity>

Strider
  • 4,452
  • 3
  • 24
  • 35
  • 3
    This completely removes the app from the recents list, which is not what I want – Rajath Apr 15 '15 at 06:13
  • 4
    hmm... well you could maybe overlay your activity with one big black image, and make that one **Visible** in your `onPause()` and make it **Gone** in your `onResume()` – Strider Apr 15 '15 at 06:20
  • 4
    @Strider that doesn't work, because the thumbnail is created before the `onPause()` method is called – mguellsegarra Nov 18 '17 at 12:05
1

If your Activity is showing sensitive data, for the security reason is better to don't allow user to make screenshots. If you are worry even about recent app screen, why you are not worry about screenshots on the same time? I think, that Android OS SecureFlag is designed to protect your data, and screenshots ability, just negates it's purpose.
If you anyway, want this ability, you can try to move all your sensitive data to separate activity with this flag. In that case, you will protect your sensitive data, and in other app's activities will have ability to make screenshots.

VadymVL
  • 5,366
  • 3
  • 26
  • 41
  • 1
    I fully agree with your reasoning. Unfortunately, that's the requirement for the app, and I need to see if it is at all possible. – Rajath Apr 15 '15 at 06:44
1

After doing some research I have found this to be impossible at this point.

The onCreateThumbnail seems to be the function you're looking for. Unfortunately this function seems to be unimplemented or at least not working.

As Ped7g pointed out: The onCreateThumbnail is "won't fix" since 2014.

It's also flagged deprecated for removal in the Android onCreateThumbnail reference page

This functionality will most likely never work.

Robbe Roels
  • 195
  • 4
  • 11
  • 1
    The `onCreateThumbnail` is *"won't fix"* since 2014... I wouldn't count on this one to be working in near future. – Ped7g Jul 12 '18 at 06:36
  • Which I also stated "Unfortunately this function seems to be unimplemented or at least not working." – Robbe Roels Jul 12 '18 at 11:32
  • "not working" IMO is not as strong as "deliberately decided by Google to stay in non-working state"... sadly. Sounds like nifty functionality, with only some mild abuse potential, but I don't understand Android OS API design decisions since the very start of 1.2, so no wonder they are going in directions I don't like (btw, thanks for your information, it was helpful for me, I was just trying to emphasize the real state of this thing, it's "dead", highly likely forever). – Ped7g Jul 12 '18 at 11:55
  • Fair enough, thank you for your comment, I'll edit this in the answer – Robbe Roels Jul 13 '18 at 07:33