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

1

6

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).

Integrate answered 23/10, 2019 at 15:31 Comment(3)
Did you find the answer to the question?Jezebel
No.. in the end I leave the white activity preview. :(Integrate
is there an answer for this question?Naamana
C
8

You can 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)

Culottes answered 12/11, 2021 at 14:41 Comment(2)
Hi Sambhav, but using this solution we are not able to take a screenshot inside the app. How can we "clear" FLAG_SECURE when we are inside onResume? TnksNarration
@FrancescoBocci you can just give in some random tag when u r in onResume. That will clear the FLAG_SECURECulottes

© 2022 - 2024 — McMap. All rights reserved.