4

I'm currently developing a light-weight application where in one part of the app I would like to COMPLETELY take away the ability to take a screenshot in Android. When I say screenshot, I'm talking about iPhone's "screen capture" feature. This is for security reasons. I realize there are apps out there that allow users in Android to do this as well, and I want to block this functionality. Any way of doing this is fine, whether disabling the hardware buttons, freezing the phone, or via software.

Charles
  • 50,010
  • 13
  • 100
  • 141
datWooWoo
  • 842
  • 1
  • 11
  • 41
  • 2
    For ICS and up, have you tried [this](http://commonsware.com/blog/2012/01/16/secure-against-screenshots.html)? For everything older, mileage will vary since there wasn't a standard screenshot implementation. – A--C Aug 22 '13 at 23:40

3 Answers3

4

You can use the window LayoutParam FLAG_SECURE. Add this to your onCreate method:

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

More on this topic can be found here.

Community
  • 1
  • 1
Phil
  • 35,089
  • 23
  • 123
  • 159
1

Use WindowManager.LayoutParams.FLAG_SECURE before setContentView() method and you have to add this code in all the activities that you want to protect.

public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
            WindowManager.LayoutParams.FLAG_SECURE);
    setContentView(R.layout.activity_main);
}
Nordle
  • 2,698
  • 3
  • 12
  • 32
Chandra Pal
  • 11
  • 1
  • 1
0

Add an application class in your project like that to prevent the full app from screen-shot (All Activity and Fragment around your app). This is the example project and below I mentioned the core code -

public class MyApplicationContext extends Application {

    private  Context context;

    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        setupActivityListener();
    }

    private void setupActivityListener() {
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);            }
            @Override
            public void onActivityStarted(Activity activity) {
            }
            @Override
            public void onActivityResumed(Activity activity) {

            }
            @Override
            public void onActivityPaused(Activity activity) {

            }
            @Override
            public void onActivityStopped(Activity activity) {
            }
            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
            }
            @Override
            public void onActivityDestroyed(Activity activity) {
            }
        });
    }
}
Gk Mohammad Emon
  • 4,390
  • 2
  • 35
  • 34