androidstatusbarandroid-windowmanagerandroid-statusbarandroid-window

Change status bar color without having Window


I want to manipulate the status bar (ex.color) but in the background. I am using a foreground service to do this. Thus there is no window as it is happening in the background, specifically there is no activity. However, the function

public abstract void setStatusBarColor (int color)

is called by the abstract class Window: https://developer.android.com/reference/android/view/Window.html

And so because it is abstract I cannot initiate it and I cant use getWindow() as I do not implement an activity class. The following answer uses Activity. Is there another way to implement this? How to change the status bar color in android


Solution

  • And so because it (setStatusBarColor()) is abstract I cannot initiate it and I can't use getWindow()

    You should not create an instance of Window class on your own, that's something that you should fetch from framework, specifically from the activity. As long as you do not have an activity, you can't get a reference to a Window instance.

    Assuming the process of your app is not in foreground (i.e. there is no any visible activity), then you have no ways to change the color of the status bar.

    Imagine the framework would allow to do such things, then it might get misused by malicious apps to randomly change the status bar color each second, when user hasn't even opened the malicious app. I think that would be considered as a flaw, not a feature.