Search Issue Tracker

By Design

Votes

1

Found in

2021.3.33f1

2022.3.15f1

2023.2.4f1

2023.3.0a19

6000.0.0b11

Issue ID

UUM-58988

Regression

No

[Android] Unity overrides the flags set in the MainActivity.java file of a Native Android application

--

-

Reproduction steps:
1. Create a new Unity project
2. In Build Settings, click Export project
3. Incorporate it into a Native Android application (Unity as a Library) according to [Unity’s official Github tutorial|https://github.com/Unity-Technologies/uaal-example/blob/master/docs/android.md]
4. Set the flag: {{getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);}} in the OnCreate or OnStart method of the MainActivity.java
5. Ensure to set your Android device screen timeout to the lowest (e.g 15secs)
6. Build and Run on Android
7. On the app select either “Show Unity" or "Show UnityGameActivity”
8. Observe the app’s screen for more than 15secs ( or the amount of screen timeout you used)

Expected result: The screen remains on
Actual result: The screen turns off after a couple of seconds depending on the screen timeout on the device

Reproducible with: 2021.3.33f1, 2022.3.15f1, 2023.2.4f1, 2023.3.0a19

Reproducible with these devices:
VLNQA00518 - Google Pixel 4 (Pixel 4), CPU: Snapdragon 855 SM8150, GPU: Adreno 640, OS: 12
VLNQA00414 - Galaxy Note10+ 5G (SM-N976V), CPU: Snapdragon 855 SM8150, GPU: Adreno 640, OS: 9 VLNQA00278 - Xiaomi Redmi Note 7 (Redmi Note 7), CPU: Snapdragon 660, GPU: Adreno 512, OS: 9.0.0
VLNQA00139 - Vivo Xplay6 (vivo Xplay6), CPU: Snapdragon 820 MSM8996, GPU: Adreno 530, OS: 7.1.1
VLNQA00132 - Xiaomi Mi 5s (MI 5s), CPU: Snapdragon 820 MSM8996, GPU: Adreno 530, OS: 6.0.1

Testing environment: macOS(Intel) 14.0 Sonoma, Android Studio Flamingo | 2022.2.1 Patch 1

Notes: If you stay on the Native app screen(i.e the one that show ups when the app is launched) the app obeys the set flag but as soon as you move to a Unity scene the flag is overridden.
This could be as a result of code found in UnityPlayer.class:

{noformat}private void hideStatusBar() {
Activity var1;
if ((var1 = this.mActivity) != null) {
var1.getWindow().setFlags(1024, 1024);
}
}{noformat}

  1. Resolution Note:

    Hello. I am sorry for the inconvenience you might have.

    * Removing hideStatusBar(): Even if you remove hideStatusBar() API it will not show the status bar. It is controlled in the C++ source
    To show it (https://docs.unity3d.com/Manual/class-PlayerSettingsAndroid.html#Resolution),
    1. You need to Go to Edit > Project Settings.. and select Android tab
    2. Go to Resolution and Presentation *section and uncheck ‘*Hide Navigation Bar’
    Though, it will show Navigation Bar at the bottom as well. If you want to separate Navigation Bar and Status Bar, it will be a feature request and it might take time and I am not sure if I am allowed to backport it.

    * FLAG_KEEP_SCREEN_ON: As you mentioned, you can make it by setting Screen.sleepTimeout to SleepTimeout.NaverSleep. To control the value of timeout you need to use Android API (https://developer.android.com/reference/android/provider/Settings.System#SCREEN_OFF_TIMEOUT) and for that, you need to add WRITE_SETTINGS permission (https://developer.android.com/reference/android/Manifest.permission#WRITE_SETTINGS) into AndroidManifest.xml file. (Though, I read somewhere that someone said Android Studio complains "Permission is only granted to system apps". )

    Note: onResume() in MainUnityActivity.java is called before Start() in Cube.cs. So if you set timeout value in onResume() of MainUnityActivity.java and add Screen.sleepTimeout = SleepTimeout.NeverSleep; to Start() in Cube.cs, SleepTimeout.NeverSleep will the final selection. If you don’t set the value of Screen.sleepTimeout to SleepTimeout.NeverSleep in Cube.cs, the setting from Android will work. Or it might be better to add callback on Androidside and control it inside the callback.

    * Dim screen - I think you can use Screen.brightness (https://docs.unity3d.com/2023.3/Documentation/ScriptReference/Screen-brightness.html)

    I cannot guarantee but we might update this in the future (And this is a feature request, so it will take some time when we do it.) Thank you.

Add comment

Log in to post comment

All about bugs

View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.