Android 和dialog 防录屏功能失效,游戏防录屏功能失效.

1. 正常禁止录屏在Activity的onCreate()方法中调用如下代码,但是在小米,OPPO等设备上只能禁止截屏,无法禁止录屏.

 @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
    } 

2. 如果上面方法失效可以使用下面的方法,也是在Activity的onCreate()方法调用.

  @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.flags = WindowManager.LayoutParams.FLAG_SECURE;
        getWindow().setAttributes(params);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
    }

3.Android Dialog设置上面的属性是无效的,可以在BaseDialog或当前Dialog的onCreate()方法增加如下代码,就是给当前dialog窗口单独设置禁止录屏属性.

 WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
 params.flags = WindowManager.LayoutParams.FLAG_SECURE;
 getDialog().getWindow().setAttributes(params);

4.游戏禁止录屏失效,打开SurfaceView的安全密码模式即可,但是会导致在游戏中全程都禁止录屏.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //一定要在父类onCreate方法之后调用
        if (getGLSurfaceView() != null) getGLSurfaceView().setSecure(true);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);

    }
getGLSurfaceView()方法是获取父类 Cocos2dxActivity 方法中的私有变量,或者把父类的 mGLSurfaceView 变量从 private 变成 protected,那上面的方法也可以使用

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (mGLSurfaceView != null) mGLSurfaceView.setSecure(true);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
    }
5.如果在游戏中有时可以录屏,有时不可以录,第4种方法就不满足了,但是安卓的禁止录屏功能对游戏使用的SurfaceView不生效,那就可以在总布局之上再悬浮一个ImageView或TextView,不给他设置任何内容,并把它设置成透明,这样对Actvity设置禁止录屏就可以生效了.

        找到游戏的CocosdxActivity,在里面找到布局,他使用的是FrameLayout或ResizeLayout(继承自FrameLayout), 在他添加完SurfaceView页面之后,添加一个ImageView即可.

// 修复SurfaceView处于顶层时WindowManager.LayoutParams.FLAG_SECURE失效的问题
        ImageView imageView = new ImageView(this);
        imageView.setAlpha(0.0f);
        mFrameLayout.addView(imageView);

猜你喜欢

转载自blog.csdn.net/zhao8856234/article/details/127784669