简体   繁体   中英

Prevent screen capture in Unity Android app

I'm trying to create a Unity Android plugin to prevent screenshots while running the app. I read the following.

How do I prevent Android taking a screenshot when my app goes to the background?

From what I have read I need to call the following in the Activity onCreate handler....

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

But I have not been able to get this to work. I have tried extending UnityPlayerActivity, UnityPlayerNativeActivity, and even android.app.NativeActivity. I have verified that the plugin itself is working properly (I am able to call other functions in the plugin) and the onCreate method is getting executed.

I have tried setting the flags in multiple places inside onCreate, such as before and/or after setContentView(mUnityPlayer), but it never works.

I'm testing on an HTC One m8. Anyone know what the problem could be?

Put this in your onCreate() method:

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

The problem might be your reference, this worked fine when I tested it:

public class FlagSecureTestActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

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

setContentView(R.layout.main);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM