16

I was wondering if there is a way to detect through some "onXXXXX" call-back method or received broadcast if some other process is about to take a screen-shot of my app's display. For example if the SDK tools or some other screen capturing app performs a "Screen Capture", I would like to be notified and then decide if I should allow or disallow the screen capture.

If this is not possible is there a way to lock the display so no other process can screen capture my display?

Ken White
  • 123,280
  • 14
  • 225
  • 444
burgwindeck
  • 163
  • 1
  • 1
  • 5

2 Answers2

25

This worked for me on a Samsung Tab 2 7"

Using the following approach, to simply add the FLAG_SECURE to the Window Flags

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

with the caveat that you may need to change

LayoutParams

to

WindowManager.LayoutParams
Andrea Lazzarotto
  • 2,471
  • 1
  • 22
  • 38
user330844
  • 872
  • 1
  • 12
  • 12
7

There is no supported method to take a screenshot of another app on Android. The only ways involve either rooting or using the SDK, which both offer little to no chance of you either blocking or receiving notification of it. If another app takes your screenshot, it is by default using non-supported methods and can probably do as it pleases.

I'd like to clarify that I am not implying you shouldn't use any methods available to secure your app. I'm just letting you know that it is probably impossible to do so without using non-supported methods, and that even if you do use them you may not be 100% secure against screenshots.

jakebasile
  • 8,084
  • 3
  • 28
  • 34
  • Actually, there is the start of such capability in the official sources, and it's usable on a number of vendor devices by non-root apps. – Chris Stratton Jul 20 '11 at 15:48
  • Still, these are not official until they are in the API documentation. I am simply saying that because any method of taking a screenshot is currently either unsupported or directly from the SDK, there are very likely no official methods to interact with it. – jakebasile Jul 20 '11 at 15:52
  • What about `View.getDrawingCache(boolean autoScale)`? The doc reads: _Returns the bitmap in which this view drawing is cached._ – iDroid Jul 20 '11 at 15:56
  • @Jake Basile - when the desire is to **block** something, it doesn't matter if the capability is official, it only matters if it works - if it does, you can bet someone is using it. We seem to agree that it's not possible to fully close the door. However, if/when there is an official screenshot capability, it probably will have a mechanism for blocking password fields, etc. However it would be a mistake to limit a security analysis to official capabilities. – Chris Stratton Jul 20 '11 at 15:59
  • @Chris Stratton Of course it should be considered, but I am not saying that it shouldn't. I'm just saying that there probably isn't any official support any way about screenshots and that blocking them would require similarly unsupported methods. – jakebasile Jul 20 '11 at 16:08
  • @iDroid that will return the drawing cache of a view, but it would only be callable by your own application, not by some third party (unless you opened it up in some way, but then you'd still have control). I don't think the OP is concerned about taking his own screenshots. – jakebasile Jul 20 '11 at 16:09
  • The modern DDMS screen capture is based on a surface flinger API, which actually does appear to block capture of "secure windows" - see http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=services/surfaceflinger/SurfaceFlinger.cpp However a number of vendors do something more efficient using the GPU and skipping that check. – Chris Stratton Jul 20 '11 at 16:28
  • 1
    @Chris Stratton -- Jake's answer is spot on; the OP wanted to know about official security mechanisms. – cdhabecker Jul 20 '11 at 17:05
  • @cdhabecker - There is nothing in the question limiting to official means of protection; practically speaking, the problem is to combat both official and unofficial means of screenshots ("or some other screen capturing app") which makes it a problem without a real solution. – Chris Stratton Jul 20 '11 at 17:07