这里主要用到了原生模块,下面贴出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); // 禁止截屏
}
});
}
}
然后在componentDidMount和componentWillUnmount两个生命周期中分别调用setFlag和clearFlag这两个方法就可以了。