2
\$\begingroup\$

I am using Unity. I want to set the status bar to be permanent in opaque black. I set Start in Full Screen Mode in the Player Settings to false, and call ShowStatusBar from the following code, and life is good:

using System;
using UnityEngine;

public static class AndroidUtility
{
    private const int MinStatusBarColorApi = 21;
    private const int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
    private const int BlackColor = 0x00000000;

    private static AndroidJavaObject activity;

    public static void ShowStatusBar(int color = BlackColor)
    {
        RunOnUiThread(() =>
            {
                using (var window = Window)
                {
                    window.Call("clearFlags", SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
                    if (GetApi() >= MinStatusBarColorApi)
                    {
                        window.Call("setStatusBarColor", color);
                    }
                    else if (color != default)
                    {
                        Debug.LogWarning("Changing the status bar color is not supported on Android API lower than Lollipop.");
                    }
                }
            });
    }

    private static void RunOnUiThread(Action action)
    {
        Activity.Call("runOnUiThread", new AndroidJavaRunnable(action));
    }

    private static AndroidJavaObject Activity
    {
        get
        {
            if (activity == null)
            {
                var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
                activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
            }
            return activity;
        }
    }

    private static AndroidJavaObject Window
    {
        get
        {
            return Activity.Call<AndroidJavaObject>("getWindow");
        }
    }

    private static int GetApi()
    {
        using (var version = new AndroidJavaClass("android.os.Build$VERSION"))
        {
            return version.GetStatic<int>("SDK_INT");
        }
    }
}

I then added a plugin (Firebase Messaging) which uses a custom AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.mypackage" android:versionCode="1" android:versionName="1.0">
  <application android:label="@string/app_name" android:icon="@drawable/app_icon">
    <!-- The MessagingUnityPlayerActivity is a class that extends
         UnityPlayerActivity to work around a known issue when receiving
         notification data payloads in the background. -->
    <activity android:name="com.google.firebase.MessagingUnityPlayerActivity" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
      <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
    </activity>
    <service android:name="com.google.firebase.messaging.MessageForwardingService" android:exported="false" />
  </application>
</manifest>

My AndroidUtility class no longer works! The status bar just shows up as a solid white bar. I suspect this is because I am still referencing the default UnityPlayerActivity, when I should be referencing the MessagingUnityPlayerActivity? I have tried changing the AndroidJavaClass to com.google.firebase.MessagingUnityPlayerActivity, (among other things,) but that just makes the status bar go away completely.

How can I adjust the AndroidUtility class and AndroidManifest to work with the inherited AndroidManifest.xml?

\$\endgroup\$
  • \$\begingroup\$ It looks to me like your real question is "how can I show the status bar when using Firebase" — and referencing an inherited activity is one guess at a solution, but not necessarily the best solution. I'd recommend asking about your root problem/end goal, rather than the stepping stone you hope will be part of the solution. \$\endgroup\$ – DMGregory Jun 21 at 16:58
  • \$\begingroup\$ Related: stackoverflow.com/questions/63210429/… \$\endgroup\$ – Evorlor Aug 4 at 23:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.