Steve Krichten
Steve Krichten

Reputation: 61

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?

Upvotes: 1

Views: 2578

Answers (1)

Mingan Beyleveld
Mingan Beyleveld

Reputation: 281

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);
  }
}

Upvotes: 1

Related Questions