Anand
Anand

Reputation: 3406

How to prevent Screen Capture in Android

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).

Upvotes: 196

Views: 258936

Answers (16)

Ozzini
Ozzini

Reputation: 145

Regarding to blocking the possibility of taking a screenshot in the application:

Window window = requireActivity().getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_SECURE);

remember to clean this flag (f.e. in onDestroy) - to enable screenshots in other safe places of the app:

window.clearFlags(WWindowManager.LayoutParams.FLAG_SECURE);

Upvotes: 2

Ido Naveh
Ido Naveh

Reputation: 2492

Just add this line:

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

Before your setContentView() method.

Upvotes: 104

RobCob
RobCob

Reputation: 2223

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

Upvotes: 213

Raviraj
Raviraj

Reputation: 926

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

Upvotes: 2

user5583316
user5583316

Reputation:

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

Window.SetFlags(WindowManagerFlags.Secure, WindowManagerFlags.Secure);

Upvotes: 3

S.Bozzoni
S.Bozzoni

Reputation: 1002

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

Upvotes: 8

Gk Mohammad Emon
Gk Mohammad Emon

Reputation: 6938

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">

Upvotes: 45

Swapnil Patil
Swapnil Patil

Reputation: 200

Kotlin users can add following code in Application class to prevent app screenshots

    class MyApplication : Application() {
    
    override fun onCreate() {
      
        super.onCreate()
       
        registerActivityLifecycle()
       
    }

    private fun registerActivityLifecycle() {
        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                activity.window.setFlags(
                    WindowManager.LayoutParams.FLAG_SECURE,
                    WindowManager.LayoutParams.FLAG_SECURE
                )
            }

            override fun onActivityStarted(activity: Activity) {}
            override fun onActivityResumed(activity: Activity) {}
            override fun onActivityPaused(activity: Activity) {}
            override fun onActivityStopped(activity: Activity) {}
            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}
            override fun onActivityDestroyed(activity: Activity) {}
        })
    }
}

Upvotes: 3

Anton L.
Anton L.

Reputation: 798

Easy and elegant:

import android.view.WindowManager.LayoutParams;

// ...

@Override
public void onWindowFocusChanged(boolean isFocused) {
  if (isFocused) {
    getWindow().clearFlags(LayoutParams.FLAG_SECURE);
  } else {
    getWindow().setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE);
  }
  super.onWindowFocusChanged(isFocused);
}

Upvotes: 0

Diego Palomar
Diego Palomar

Reputation: 7061

For those that have to disable screenshots and are using Jetpack Compose this is what you need:

fun Activity.disableScreenshots() {
  window.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
}

fun Activity.enableScreenshots() {
  window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
}

@Composable
fun DisableScreenshots() {
  val context = LocalContext.current
  DisposableEffect(Unit) {
    context.getActivity()?.disableScreenshots()
    onDispose {
      context.getActivity()?.enableScreenshots()
    }
  }
}

Then just invoke DisableScreenshots from your composable function ;)

Upvotes: 0

Ajay T P
Ajay T P

Reputation: 329

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)

Upvotes: 31

Rajesh Shetty
Rajesh Shetty

Reputation: 515

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.

Upvotes: 38

Jithu P.S
Jithu P.S

Reputation: 1843

Try this:

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

Upvotes: 11

Soo Chun Jung
Soo Chun Jung

Reputation: 595

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.

Upvotes: 1

r5d
r5d

Reputation: 573

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

Upvotes: 3

rupesh jain
rupesh jain

Reputation: 3430

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.

Upvotes: 12

Related Questions