Android How to Determine If a Page Is Full Screen

  • 2021-12-12 05:28:38
  • OfStack

Android wants to judge whether Activity is full screen, and finds some methods on the Internet. It is useless to directly obtain flags and compare it with a specific value. In fact, it should be wrong after analysis. Most of them are about how to set full screen and cancel full screen, and there is no way to judge whether it is full screen.

In fact, full-screen control is either set through the theme, or the code addFlags, and will eventually go to the setFlags method of Window. See the source code below:


public void setFlags(int flags, int mask) {
  final WindowManager.LayoutParams attrs = getAttributes();
  attrs.flags = (attrs.flags&~mask) | (flags&mask);
  mForcedWindowFlags |= mask;
  dispatchWindowAttributesChanged(attrs);
}

The main logic is this sentence:


attrs.flags = (attrs.flags&~mask) | (flags&mask)

It is a bit operation. Look at the flag constant that can be set in attrs under 1


public static final int FLAG_ALLOW_LOCK_WHILE_SCREEN_ON  = 0x00000001;
public static final int FLAG_DIM_BEHIND          = 0x00000002;
public static final int FLAG_BLUR_BEHIND         = 0x00000004;
public static final int FLAG_NOT_FOCUSABLE        = 0x00000008;
public static final int FLAG_NOT_TOUCHABLE        = 0x00000010;
public static final int FLAG_NOT_TOUCH_MODAL       = 0x00000020;
public static final int FLAG_TOUCHABLE_WHEN_WAKING    = 0x00000040;
public static final int FLAG_KEEP_SCREEN_ON        = 0x00000080;
public static final int FLAG_LAYOUT_IN_SCREEN       = 0x00000100;
public static final int FLAG_LAYOUT_NO_LIMITS       = 0x00000200;
public static final int FLAG_FULLSCREEN          = 0x00000400;
public static final int FLAG_FORCE_NOT_FULLSCREEN     = 0x00000800;
public static final int FLAG_DITHER            = 0x00001000;
public static final int FLAG_SECURE            = 0x00002000;
public static final int FLAG_SCALED            = 0x00004000;
public static final int FLAG_IGNORE_CHEEK_PRESSES     = 0x00008000;
public static final int FLAG_LAYOUT_INSET_DECOR      = 0x00010000;
public static final int FLAG_ALT_FOCUSABLE_IM       = 0x00020000;
public static final int FLAG_WATCH_OUTSIDE_TOUCH     = 0x00040000;
public static final int FLAG_SHOW_WHEN_LOCKED       = 0x00080000;
public static final int FLAG_SHOW_WALLPAPER        = 0x00100000;
public static final int FLAG_TURN_SCREEN_ON        = 0x00200000;
public static final int FLAG_DISMISS_KEYGUARD       = 0x00400000;
public static final int FLAG_SPLIT_TOUCH         = 0x00800000;
public static final int FLAG_HARDWARE_ACCELERATED     = 0x01000000;
public static final int FLAG_LAYOUT_IN_OVERSCAN      = 0x02000000;
public static final int FLAG_TRANSLUCENT_STATUS      = 0x04000000;
public static final int FLAG_TRANSLUCENT_NAVIGATION    = 0x08000000;
public static final int FLAG_LOCAL_FOCUS_MODE       = 0x10000000;
public static final int FLAG_SLIPPERY           = 0x20000000;
public static final int FLAG_LAYOUT_ATTACHED_IN_DECOR   = 0x40000000;
public static final int FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS = 0x80000000;

Obviously, if these 106-ary values are changed to binary, there is only one 1, and the 1 of each variable is in a different position, so setting an flag is definitely setting the corresponding position of flags to 1, while clearFlags is setting the corresponding position to 0.

For example, the value of FLAG_FULLSCREEN is 0x00000400. If it is changed to binary, the next bit is 0100 0000 0000, which controls the 11th bit from right to left. When addFlags, the two parameters flags and mask are the same. So (attrs. flags & ~mask) | (flags & flags of mask & mask does AND operation with itself, and the result is still itself, while ~ mask ends with 1011 1111 1111, and the original flags does AND operation, then the 11th bit must become 0, and other bits do AND operation with 1, keeping it as it is, and then doing OR with the following, then the 11th bit becomes 1 again, and this operation will only affect the 11th bit, and other positions remain unchanged, regardless of whether the original 11th bit is 0 or 1, the result will become 1.

Similarly, if it is clearFlags, the first parameter becomes 0, and the second parameter is FLAG_FULLSCREEN, so (flags & mask) must be 0, just look ahead, (attrs. flags & ~ mask) The 11th bit of this operation must be 0.

In fact (attrs. flags & ~mask) | (flags & mask) The previous AND operation will change the corresponding position to 0, and then look at the subsequent AND operation, which will result in the corresponding position being 1, which will eventually be 1, and the subsequent operation is 0, which will eventually be 0. And the latter control is 0 or make an AND operation with yourself.

I said a lot of nonsense, which is actually the most basic bit operation. So it is very simple to judge whether it is full screen, just look at whether the 11th bit of flags from right to left is 0 or 1, and just make a logical AND with FLAG_FULLSCREEN, except the 11th bit, all other bits become 0.

The judgment method is:


if ( (getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN)
          == WindowManager.LayoutParams.FLAG_FULLSCREEN) {
  //  It's full screen 
}

It's over here.

The above is Android how to judge whether the page is full-screen details, more about Android judge whether the page is full-screen information please pay attention to other related articles on this site!


Related articles: