141

Is it possible to prevent the screen recording in Android Application?

I would like to develop an Android Secure Application. In that I need to detect screen recording software which are running background and kill them. I have used SECURE FLAG for prevent screenshots. But I dont know is it possible to prevent Video capturing of Android Screen also. Let me know how to prevent screen capturing (video / screenshots).

Mck
  • 109
  • 1
  • 10
Anand
  • 2,743
  • 9
  • 32
  • 51
  • 8
    - You should remember one can always point a camera on the screen to steal the content - There some techniques used in the DRM field but they are pretty complex and I don't really know what's going on under the hood – Shai Levy Feb 19 '15 at 12:49
  • 10
    The concept that @ShaiLevy explains, is usually called Analog Hole https://en.wikipedia.org/wiki/Analog_hole which explains the innevitable security hole of media transmission. – gusridd Jul 20 '15 at 22:01

12 Answers12

151

I'm going to say that it is not possible to completely prevent screen/video capture of any android app through supported means. But if you only want to block it for normal android devices, the SECURE FLAG is substantial.

1) The secure flag does block both normal screenshot and video capture.

Also documentation at this link says that

Window flag: treat the content of the window as secure, preventing it from appearing in screenshots or from being viewed on non-secure displays.

Above solution will surely prevent applications from capturing Video of your app

See the answer here.

2) There are alternative means of capturing screen content.

It may be possible to capture the screen of another app on a rooted device or through using the SDK,

which both offer little to no chance of you either blocking it or receiving notification of it.

For example: there exists software to mirror your phone screen to your computer via the SDK and so screen capture software could be used there, undiscoverable by your app.

See the answer here.

getWindow().setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE);
stevyhacker
  • 1,646
  • 1
  • 19
  • 29
RobCob
  • 1,603
  • 1
  • 10
  • 10
80

Just add this line:

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

Before your setContentView() method.

Vishal Yadav
  • 3,351
  • 3
  • 20
  • 40
Ido Naveh
  • 2,136
  • 1
  • 18
  • 46
  • 11
    Question : [quote]I have used SECURE FLAG[unquote] Your "answer" : [quote]You can use FLAG_SECURE[unqote] Can you spot a problem here ? – 2Dee Jul 20 '15 at 14:46
  • Hi,I am developing a web based portal application. The users may use any kind of browsers to reach this portal. The users of this portal should not take the screenshots when they are using mobile devices. Can I use the same precautions (etWindow().setFlags(LayoutParams.FLAG_SECURE,LayoutParams.FLAG_SECURE);) for the web application. Does it work for IOS? If I can use the same type of precautions where should I call this line? – Ferda-Ozdemir-Sonmez Apr 17 '17 at 12:31
  • There is no need to do this before setContentView() – laim2003 Aug 23 '20 at 19:05
29

To disable Screen Capture:

Add following line of code in onCreate() method:

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

To enable Screen Capture:

Find for LayoutParams.FLAG_SECURE and remove the line of code.

Vishal Yadav
  • 3,351
  • 3
  • 20
  • 40
Rajesh Shetty
  • 397
  • 4
  • 5
18

For Java users write this line above your setContentView(R.layout.activity_main);

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

For kotlin users

window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)
Abhi
  • 2,758
  • 1
  • 22
  • 37
user9778021
  • 189
  • 1
  • 4
  • for kotlin, depending on the context, that should be `window?.set...` because on Dialogs, you don't know if you're going to have a Window. – Martin Marconcini Aug 26 '19 at 15:08
  • 1
    The original question already said they've used secure flag and your answer is to use secure flag. This doesn't really answer the question posted. – Subaru Tashiro Jan 08 '20 at 05:54
18

I saw all of the answers which are appropriate only for a single activity but there is my solution which will block screenshot for all of the activities without adding any code to the activity. First of all make an Custom Application class and add a registerActivityLifecycleCallbacks.Then register it in your manifest.

MyApplicationContext.class

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



}

Manifest

 <application
        android:name=".MyApplicationContext"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
Zoe
  • 23,712
  • 16
  • 99
  • 132
Gk Mohammad Emon
  • 2,407
  • 2
  • 20
  • 28
11

You can make your app as device/profile owner and call setScreenCaptureDisabled(). From the docs, this api does the following:

public void setScreenCaptureDisabled (ComponentName admin, boolean disabled) Added in API level 21

Called by a device/profile owner to set whether the screen capture is disabled. Disabling screen capture also prevents the content from being shown on display devices that do not have a secure video output. See FLAG_SECURE for more details about secure surfaces and secure displays.

The calling device admin must be a device or profile owner. If it is not, a security exception will be thrown. Parameters admin Which DeviceAdminReceiver this request is associated with. disabled Whether screen capture is disabled or not.

Alternatively you can become an MDM(Mobile Device Management) partner app.OEMs provides additional APIs to their MDM partner apps to control the device.For example samsung provides api to control screen recording on the device to their MDM partners.

Currently this is the only way you can enforce screen capture restrictions.

rupesh jain
  • 3,287
  • 1
  • 10
  • 21
10

Try this:

getWindow().setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE);
FelixSFD
  • 5,456
  • 10
  • 40
  • 106
Jithu P.S
  • 1,833
  • 18
  • 32
  • 3
    Question : [quote]I have used SECURE FLAG[unquote] Your "answer" : [quote]You can use FLAG_SECURE[unqote] Can you spot a problem here ? – 2Dee Jul 20 '15 at 14:46
  • Window flag: treat the content of the window as secure, preventing it from appearing in screenshots or from being viewed on non-secure displays. See FLAG_SECURE for more details about secure surfaces and secure displays. Constant Value: 8192 (0x00002000) Read more from here http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_SECURE – Jithu P.S Jul 21 '15 at 07:25
  • Please don't deface your post by rolling back our improvements – FelixSFD Apr 21 '17 at 11:33
4

I used this solution to allow manual snapshot in app while disallowing screen capture when the app goes in background, hope it helps.

@Override
protected void onResume() {
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
    super.onResume();
}

@Override
protected void onPause() {
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
    super.onPause();
}
S.Bozzoni
  • 860
  • 8
  • 15
2

According to this official guide, you can add WindowManager.LayoutParams.FLAG_SECURE to your window layout and it will disallow screenshots.

r5d
  • 579
  • 5
  • 24
0

about photo screenshot, FLAG_SECURE not working rooted device.

but if you monitor the screenshot file, you can prevent from getting original file.

try this one.

1. monitoring screenshot(file monitor) with android remote service
2. delete original screenshot image.
3. deliver the bitmap instance so you can modify.

Soo Chun Jung
  • 445
  • 3
  • 12
0

Ad this line inside the OnCreate event on MainActivity (Xamarin)

Window.SetFlags(WindowManagerFlags.Secure, WindowManagerFlags.Secure);
AlejandroAlis
  • 161
  • 2
  • 9
0
public class InShotApp extends Application {
    
        @Override
        public void onCreate() {
            super.onCreate();
            registerActivityLifecycle();
        }
    
        private void registerActivityLifecycle() {
    
            registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
                @Override
                public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
                    activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);            }
    
                @Override
                public void onActivityStarted(@NonNull Activity activity) {
    
                }
    
                @Override
                public void onActivityResumed(@NonNull Activity activity) {
    
                }
    
                @Override
                public void onActivityPaused(@NonNull Activity activity) {
    
                }
    
                @Override
                public void onActivityStopped(@NonNull Activity activity) {
    
                }
    
                @Override
                public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
    
                }
    
                @Override
                public void onActivityDestroyed(@NonNull Activity activity) {
    
                }
            });
    
        }
    }
Raviraj
  • 536
  • 6
  • 12