react native 中实现个别页面禁止截屏

这里主要用到了原生模块,下面贴出FlagSecureModule.java的代码

package com.studyproj.flagsecure;

import android.util.Log;
import android.widget.Toast;
import android.view.WindowManager; import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod; import java.util.List; public class FlagSecureModule extends ReactContextBaseJavaModule { FlagSecureModule(ReactApplicationContext reactContext) {
super(reactContext);
} @Override
public String getName() {
return "FlagSecure";
} @ReactMethod
public void setFlag() {
Toast.makeText(getReactApplicationContext(), "setFlag", Toast.LENGTH_SHORT).show();
getCurrentActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
getCurrentActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE); // 禁止截屏
}
});
} @ReactMethod
public void clearFlag() {
Toast.makeText(getReactApplicationContext(), "clearFlag", Toast.LENGTH_SHORT).show();
getCurrentActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
getCurrentActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE); // 禁止截屏
}
});
}
}

然后在componentDidMountcomponentWillUnmount两个生命周期中分别调用setFlagclearFlag这两个方法就可以了。

上一篇:mui卡片视图的制作


下一篇:linux下find命令-atime,-ctime,-mtime真正含义