简体   繁体   English

Android如何防止截屏

[英]How to prevent Screen Capture in Android

Is it possible to prevent the screen recording in Android Application?是否可以在 Android 应用程序中阻止屏幕录制?

I would like to develop an Android Secure Application.我想开发一个 Android 安全应用程序。 In that I need to detect screen recording software which are running background and kill them.因为我需要检测正在后台运行的屏幕录制软件并杀死它们。 I have used SECURE FLAG for prevent screenshots.我使用 SECURE FLAG 来防止截屏。 But I dont know is it possible to prevent Video capturing of Android Screen also.但我不知道是否也可以阻止 Android 屏幕的视频捕获。 Let me know how to prevent screen capturing (video / screenshots).让我知道如何防止屏幕捕获(视频/屏幕截图)。

I'm going to say that it is not possible to completely prevent screen/video capture of any android app through supported means.我要说的是,不可能通过支持的方式完全阻止任何 Android 应用程序的屏幕/视频捕获。 But if you only want to block it for normal android devices, the SECURE FLAG is substantial.但是,如果您只想为普通的android 设备阻止它,那么 SECURE FLAG 就足够了。

1) The secure flag does block both normal screenshot and video capture. 1)安全标志确实阻止了正常的屏幕截图和视频捕获。

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. 2)有捕获屏幕内容的替代方法。

It may be possible to capture the screen of another app on a rooted device or through using the SDK,可以在有 root 权限的设备上或通过使用 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.例如:存在通过 SDK 将您的手机屏幕镜像到您的计算机的软件,因此可以在那里使用屏幕捕获软件,您的应用程序无法发现。

See the answer here .请参阅此处的答案。

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

Just add this line:只需添加这一行:

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

Before your setContentView() method.在您的setContentView()方法之前。

To disable Screen Capture:要禁用屏幕捕获:

Add following line of code in onCreate() method:onCreate()方法中添加以下代码行:

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.查找LayoutParams.FLAG_SECURE并删除代码行。

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.首先创建一个自定义应用程序类并添加一个registerActivityLifecycleCallbacks 。然后在您的清单中注册它。

MyApplicationContext.class 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">

For Java users write this line above your setContentView(R.layout.activity_main);对于 Java 用户,请在setContentView(R.layout.activity_main);上方写下这一行setContentView(R.layout.activity_main);

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

For kotlin users对于 kotlin 用户

window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)

You can make your app as device/profile owner and call setScreenCaptureDisabled() .您可以将您的应用设为设备/配置文件所有者并调用setScreenCaptureDisabled() From the docs , this api does the following:docs ,这个 api 执行以下操作:

public void setScreenCaptureDisabled (ComponentName admin, boolean disabled) Added in API level 21 public void setScreenCaptureDisabled (ComponentName admin, boolean disabled) 添加到 API 级别 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.有关安全表面和安全显示的更多详细信息,请参阅 FLAG_SECURE。

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.参数 admin 此请求与哪个 DeviceAdminReceiver 相关联。 disabled Whether screen capture is disabled or not. disabled 是否禁用屏幕捕获。

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.或者,您可以成为MDM(移动设备管理)合作伙伴应用程序。OEM 为其 MDM 合作伙伴应用程序提供额外的 API 以控制设备。例如,三星向其 MDM 合作伙伴提供用于控制设备屏幕录制的 API。

Currently this is the only way you can enforce screen capture restrictions.目前,这是您可以强制执行屏幕捕获限制的唯一方法。

试试这个:

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

It seems that you know how to disable screenshots from your app and there are other answers helping you on that. 看来您知道如何禁用应用程序的屏幕截图,还有其他答案可以帮助您。 But I will try to give you some important information that no one is giving you. 但是,我将尽力向您提供一些重要信息 ,而这些信息是没人能给您的。

1) You can not have an app which is 100% secure from taking screenshots (photo/video). 1)您不能拥有一个100%安全的应用程序以截取屏幕快照 (照片/视频)。 There is no official way to take screenshots in Android. 没有官方的方法来在Android中拍摄屏幕截图。 If an app is recording screens, then it must be using some non-supported methods (either rooting or using the SDK). 如果某个应用正在录制屏幕,则它必须使用某些不受支持的方法 (生根或使用SDK)。

There is very little scope for you to block an app if it is using root access to record screens. 如果应用程序使用root用户访问权限来录制屏幕,则阻止它的范围很小。

2) No one mentioned this issue here, but be very careful while using WindowManager.LayoutParams.FLAG_SECURE . 2)这里没有人提到这个问题,但是在使用WindowManager.LayoutParams.FLAG_SECURE要非常小心。 It has been verified in many devices (like on Samsung Galaxy ACE, eg GT-S5830), that this makes the entire view scrambled. 已经在许多设备(例如Samsung Galaxy ACE,例如GT-S5830)中进行了验证,这使整个视图变得混乱。 Like this, 像这样,

在此处输入图片说明

Please put a check like this, 请这样的支票

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

It works perfectly on ICS devices, so no problem there. 它可以在ICS设备上完美运行,因此没有问题。

3) I also found out that even on newer devices like Android 4.3, this causes animation problems when the screen is rotated. 3)我还发现,即使在较新的设备(例如Android 4.3)上,旋转屏幕也会导致动画问题。 Please check this bug report. 检查此错误报告。

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

根据此官方指南,您可以将WindowManager.LayoutParams.FLAG_SECURE添加到您的窗口布局中,它将禁止截图。

about photo screenshot, FLAG_SECURE not working rooted device.关于照片屏幕截图,FLAG_SECURE 无法在 root 设备上工作。

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 1. 安卓远程服务监控截图(文件监控)
2. delete original screenshot image. 2.删除原截图。
3. deliver the bitmap instance so you can modify. 3.下发位图实例,方便修改。

在 MainActivity (Xamarin) 的 OnCreate 事件中添加这一行

Window.SetFlags(WindowManagerFlags.Secure, WindowManagerFlags.Secure);
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) {
    
                }
            });
    
        }
    }

Regarding to blocking the possibility of taking a screenshot in the application:关于阻止在应用程序中截屏的可能性:

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

For those that have to disable screenshots and are using Jetpack Compose this is what you need:对于那些必须禁用屏幕截图并使用 Jetpack Compose 的人来说,这就是您所需要的:

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;)然后只需从您的可组合 function 调用DisableScreenshots ;)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM