如何禁用Android片段中的屏幕捕获? [英] How to disable screen capture in Android fragment?

查看:161
本文介绍了如何禁用Android片段中的屏幕捕获?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以禁用片段的屏幕捕获? 我知道以下内容适用于Activity类

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

但是,如果我有一个片段显示在活动顶部,该怎么办?我可以以某种方式禁用屏幕捕获吗?我试图在片段的onCreate()或onCreateView()方法中设置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.

在相关说明中,例如,我在ParentActivity.java中有一个方法(扩展了Activity)

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

}

在我的ChildFragment.java中(扩展了Fragment)

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
    }

有什么想法吗?

预先感谢

推荐答案

以下代码对我有用.

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

}

这篇关于如何禁用Android片段中的屏幕捕获?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆