這裏主要用到了原生模塊,下面貼出FlagSecureModule.java的代碼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這兩個方法就能夠了。react