是否可以从片段中禁用屏幕捕获? 我知道以下适用于Activity类
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE);
}
但是,如果我有一个显示在活动之上的片段,该怎么办?我可以以某种方式禁用屏幕捕获?我试图在片段的onCreate()或onCreateView()方法中设置FLAG_SECURE,但它不起作用。我仍然可以拍摄屏幕截图。只有当我在父活动中添加标志时,我才能禁用它。
在相关的说明中,比方说,我在ParentActivity.java中有一个方法(扩展了Activity)
public void disableScreenCapture() {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE);
}
在我的ChildFragment.java中(扩展了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
}
有什么想法吗?
提前致谢
答案 0 :(得分:5)
在片段中的disableScreenCapture()
,onResume
或onCreateView
中执行onActivityAttached
来电应该全部有效 - 他们为我做了。在onActivityCreated
中执行该调用可能不起作用,因为我相信只有在活动被重新创建时才会被调用,在它被销毁之后。但是,我没试过那个。
如果在onCreateView
中执行该调用对您不起作用,您是否100%确定您的片段实际上已添加到活动中?
对于DialogFragment
,它略有不同:
getDialog().getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE);
DialogFragment
本身不是Dialog
,而是拥有对1的引用,并在添加和删除片段时显示/解除它。对话框有自己的Windows,必须单独设置标志。
答案 1 :(得分:2)
以下代码对我有用。
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());
}