[英]How to disable screen capture in Android fragment?
是否可以禁用片段的屏幕截圖? 我知道以下內容適用於 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
}
有任何想法嗎?
提前致謝
在您的 Fragment 中的onResume
、 onCreateView
或onActivityAttached
中執行您的disableScreenCapture()
調用應該都可以工作 - 他們為我做了。 在onActivityCreated
執行該調用可能不起作用,因為我相信只有在 Activity 被銷毀后重新創建時才會調用鈎子。 不過,我沒有嘗試那個。
如果在onCreateView
執行該調用對您不起作用,您是否 100% 確定您的 Fragment 實際上已添加到 Activity 中?
對於DialogFragment
它略有不同:
getDialog().getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE);
DialogFragment
不是Dialog
本身,而是持有對一個的引用,並在添加和刪除片段時顯示/關閉它。 對話框有自己的窗口,必須單獨設置標志。
下面的代碼對我有用。
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());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.