gpt4 book ai didi

android - 如何从 Fragment 中清除 Android 中的 WindowManager 标志

转载 作者:太空宇宙 更新时间:2023-11-03 10:18:51 24 4
gpt4 key购买 nike

我为一个 Activity 设置了一个FLAG_SECURE(它包含敏感数据),但在一个特定的 fragment 中我需要清除它(因为 od Android Beam)。

Window window = getActivity().getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE);

此代码清除标志(我在 Fragment 的 onResume 回调中有它),但问题是,直到下一次配置更改(屏幕旋转,... )同样的问题是在离开 Fragment 时再次设置标志。

有谁知道,我应该怎么做才能解决这个问题? (我考虑过 Activity.recreate(),它可以工作,但我不喜欢这个解决方案)如果可能,我不想为此特定屏幕创建单独的 Activity

最佳答案

编辑:添加示例代码。

我来晚了,但我还是会发布的。这不是最好的解决方案(在我看来),因为它在 Android 4.x(5+ 是好的)上使可见的“重绘”效果,但它至少有效。我是这样使用它的:

/**
* @param flagSecure adds/removes FLAG_SECURE from Activity this Fragment is attached to/from.
*/
public void applyFlagSecure(boolean flagSecure)
{
Window window = getActivity().getWindow();
WindowManager wm = getActivity().getWindowManager();

// is change needed?
int flags = window.getAttributes().flags;
if (flagSecure && (flags & WindowManager.LayoutParams.FLAG_SECURE) != 0) {
// already set, change is not needed.
return;
} else if (!flagSecure && (flags & WindowManager.LayoutParams.FLAG_SECURE) == 0) {
// already cleared, change is not needed.
return;
}

// apply (or clear) the FLAG_SECURE flag to/from Activity this Fragment is attached to.
boolean flagsChanged = false;
if (flagSecure) {
window.addFlags(WindowManager.LayoutParams.FLAG_SECURE);
flagsChanged = true;
} else {
// FIXME Do NOT unset FLAG_SECURE flag from Activity's Window if Activity explicitly set it itself.
if (!(getActivity() instanceof YourFlagSecureActivity)) {
// Okay, it is safe to clear FLAG_SECURE flag from Window flags.
// Activity is (probably) not showing any secure content.
window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
flagsChanged = true;
}
}

// Re-apply (re-draw) Window's DecorView so the change to the Window flags will be in place immediately.
if (flagsChanged && ViewCompat.isAttachedToWindow(window.getDecorView())) {
// FIXME Removing the View and attaching it back makes visible re-draw on Android 4.x, 5+ is good.
wm.removeViewImmediate(window.getDecorView());
wm.addView(window.getDecorView(), window.getAttributes());
}
}

来源:此解决方案基于kdas的示例:How to disable screen capture in Android fragment?

关于android - 如何从 Fragment 中清除 Android 中的 WindowManager 标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29370118/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com