Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable screen capture in Android fragment?

Is it possible to disable screen capture from a fragment? I know the below works for an Activity class

onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
                WindowManager.LayoutParams.FLAG_SECURE);
}

But what if I have a fragment which shows up on top of an activity. Can I somehow disable screen capture? I've tried to set the FLAG_SECURE in the onCreate() or onCreateView() method of the fragment, but it doesn't work. I'm still able to take screen shot. Only when I add the flag in parent activity I can disable it.

On a related note, say, I've a method in ParentActivity.java (which extends Activity)

public void disableScreenCapture() {
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
            WindowManager.LayoutParams.FLAG_SECURE);

}

And in my ChildFragment.java (which extends Fragment)

            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                ParentActivity parentActivity = (ParentActivity)getActivity();
                parentActivity.disableScreenCapture(); //doesn't work
        }
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
                ParentActivity parentActivity = (ParentActivity)getActivity();
                parentActivity.disableScreenCapture(); //doesn't work either
    }

Any ideas?

Thanks in advance

like image 930
kdas Avatar asked Sep 14 '15 20:09

kdas


People also ask

How do I turn off screen capture on android?

The Project Settings dialog box appears. From the left pane, expand the Native tab, and then select the Android Mobile/Tablet sub-tab. In the Miscellaneous section, enable the Disable Application Screenshot check box. Click Done.

Is it possible to prevent screen capture?

Screen Capture protection is possible in Android App, IOS App or IOs browser, but desktop and android browsers can not prevent screen capture.


2 Answers

The below code worked for me.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    getActivity().getWindow().setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE);
    Window window = getActivity().getWindow();
    WindowManager wm = getActivity().getWindowManager();
    wm.removeViewImmediate(window.getDecorView());
    wm.addView(window.getDecorView(), window.getAttributes());

}
like image 179
kdas Avatar answered Oct 02 '22 18:10

kdas


Performing your disableScreenCapture() call in onResume, onCreateView or onActivityAttached in your Fragment should all work - they did for me. Performing that call in onActivityCreated might not work as I believe that hook is only called when the Activity is being re-created, after it's destroyed. However, I didn't try that one.

If performing that call in onCreateView isn't working for you, are you 100% certain that your Fragment is actually being added to the Activity?

For a DialogFragment it's slightly different:

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

A DialogFragment isn't a Dialog itself, but instead holds a reference to one and shows/dismisses it when the fragment is added and removed. Dialogs have their own Windows and must have the flag set individually.

like image 28
tophyr Avatar answered Oct 02 '22 19:10

tophyr