Change status bar color without having Window
Asked Answered
H

1

6

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

Hiett answered 25/1, 2018 at 15:24 Comment(8)
You mean background thread by background process, right? Process is somewhat different than thread. developer.android.com/guide/components/… You can use a handler instantiated on UI thread and post a runnable from background thread, and change color on UI thread with handler's handleMessage() method.Vanlandingham
Specifically I am using a foreground service. A service is not a thread. @Vanlandingham Sorry for the misunderstanding.Hiett
It's okay. You can still use Handler with a Service or use BroadcastReceiver with Service and listen in Activity. #9092634Vanlandingham
@Vanlandingham I am indeed using a BroadcastReceiver with Service, but what do you mean by listen in Activity? Can you please give me an example. I am actually writing my app in React-native but am using the native module they provided to implement some native code. Is is possible to manipulate the status bar inside the onReceive function ?Hiett
I added my code inside of the onReceive, the status bar color changed but only while the app was on the foreground, when it was sent to the background the color would go.Hiett
It's interesting to change back after your app paused. Normal life cycle when app is paused and send to back and brought back to screen is onPause() -> onStop() -> onStart() -> onResume(). If you are setting color in onStart() or onResume() it may revert back to default color. You can also save current color of StatusBar to SharedPreferences or onSaveInstance() method. Also statusbar color does not require a window. Get instance of statusbar inside activity and change it using statusbar.setColor() method.Vanlandingham
You will change color inside inside onReceive() method of BroadcastReceiver inside your Activity no window is involved. Get statusbar instance by findViewById or other binding then change it's color in onReceive()Vanlandingham
@Vanlandingham I cant find the statusbar.setColor() method, can you give me a link please?Hiett
I
5

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.

Iinden answered 28/1, 2018 at 10:35 Comment(2)
Thank you for your answer. However the beauty of android is that its open source. Many of Android features can be misused if you think about it. it doesn't mean they should be removed or likewise. A malicious app on the other hand can be removed form the phone. Initially I wanted to manipulate color of the icon, but the new design requires all icons to be white so I thought of status bar, any other ideas?Hiett
May windowLightStatusBar will fit your use-case.Iinden

© 2022 - 2024 — McMap. All rights reserved.