如何防止 Android 或 React-Native 应用程序中的屏幕截图

How to prevent screenshots in Android or React-Native app

如何防止用户在 React-Native 应用程序中截屏? 我在其他一些已发布问题的评论中读到有人说这行代码会阻止屏幕截图,但我到底应该在哪里插入它?

getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);

或者如果有任何其他方式请告诉我。

P.S:我有兴趣只知道Android平台。

您需要将其插入 MainActivity.java,在您的 MainActivity.java 覆盖 onCreate 方法中。

package com.reactnativepreventscreenshot;

import android.os.Bundle;
import android.view.WindowManager;

import com.facebook.react.ReactActivity;

public class MainActivity extends ReactActivity {

    /**
     * Returns the name of the main component registered from JavaScript.
     * This is used to schedule rendering of the component.
     */
    @Override
    protected String getMainComponentName() {
        return "reactnativepreventscreenshot";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
    }
}

DEMO