javaandroidsecurityscreenshot

Customize android app preview when it is in background with secure flag


In my Android app I need to use the SECURE_FLAG to say to Android: "hey you don't take a screenshoot of my app in background!". Ok it works simply using the following lines of code in my activities:

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

What I obtained is explained in the following screenshot:

screenshot

Now, How can I:

  1. Change the icon on the top of the activity preview?
  2. Change the preview of the activity, with another colour or an image?

I look for a solution, but I didn't find anything.. except that it is impossible to do it (if true I think is a pity, for Android).


Solution

  • One possible solution is to use two layouts

    1. In your xml code of the layout, make a separate layout and cut & paste the entire ui code in it and give it the id mainLayout . Make another layout which will be used for the securing UI. Give this layout the id protectLayout and there you can customize what you want like image background and etc...

    2. Then follow this in the activity you want to be secure.

      LinearLayout mainLayout,secureLayout;
      
      @Override
      protected void onPause() {
          getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
          mainLayout.setVisibility(View.GONE);
          secureLayout.setVisibility(View.VISIBLE);
          super.onPause();
      }
      
      @Override
      protected void onStart() {
          getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,WindowManager.LayoutParams.FLAG_SECURE);
          mainLayout.setVisibility(View.VISIBLE);
          secureLayout.setVisibility(View.GONE);
          super.onStart();
      }
      

    NOTE: This will best work on Android versions 11 and above as in others the display at the recent does not update immediately.

    Tested on Samsung M30s API 32 and Google Pixel API 33 Emulator