一、整個(gè)項(xiàng)目中禁止截屏與錄屏
在android/app/src/main/java/com/your-app-name/MainActivity.java
文件中的onCreate
方法中添加如下代碼即可
import android.view.WindowManager; //添加導(dǎo)入WindowManager代碼
...
@Override
protected void onCreate(Bundle savedInstanceState) {
SplashScreen.show(this);
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE); //添加禁止截屏代碼
}
...
參考:https://www.cnblogs.com/imay/p/9603287.html
二、個(gè)別頁面禁止截屏
1、創(chuàng)建一個(gè)原生模塊
創(chuàng)建一個(gè)新的Java
類并命名為FlagSecureModule.java
,放置到android/app/src/main/java/com/your-app-name/
目錄下晋控,其具體代碼如下:
package com.your-app-name;//這里寫入自己項(xiàng)目名稱
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";//標(biāo)記這個(gè)模塊,在JavaScript中通過NativeModules.ToastExample訪問到這個(gè)模塊
}
@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); // 禁止截屏
}
});
}
}
2、注冊模塊
創(chuàng)建一個(gè)新的Java
類并命名為CustomFlagSecurePackage.java
厚骗,放置到android/app/src/main/java/com/your-app-name/
目錄下,其具體代碼如下:
package com.your-app-name;//這里寫入自己項(xiàng)目名稱
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CustomFlagSecurePackage implements ReactPackage {
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
@Override
public List<NativeModule> createNativeModules(
ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new FlagSecureModule(reactContext));
return modules;
}
}
這個(gè)package
需要在MainApplication.java
文件的getPackages
方法中提供兢哭。具體路徑是: android/app/src/main/java/com/your-app-name/MainApplication.java.
// MainApplication.java
...
import com.maintainapp.CustomFlagSecurePackage; // <-- 引入包
...
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
...
new CustomFlagSecurePackage()// 添加這行
);
}
}
3领舰、在js中使用
在你想要禁止截屏的頁面添加如下代碼
import { NativeModules } from 'react-native';
...
componentDidMount() {
NativeModules.FlagSecure.setFlag();//禁止截屏
}
componentWillUnmount() {
NativeModules.FlagSecure.clearFlag();//取消截屏禁止
}