简体   繁体   English

应用程序在后台时如何更改状态栏颜色?

[英]How to change status bar color when app is in background?

I am able to change the status bar color to green but when home button is clicked when app is in background, status bar goes back to its original color.我可以将状态栏颜色更改为绿色,但是当应用程序处于后台时单击主页按钮时,状态栏会恢复到其原始颜色。 I want to retain the status bar color even when the app is in background.即使应用程序在后台,我也想保留状态栏颜色。 Like status bar is green when there is a call connected.当有呼叫连接时,状态栏是绿色的。

This is my code to change the status color.这是我更改状态颜色的代码。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    getWindow().setStatusBarColor(
        ContextCompat.getColor(context,R.color.colorAccent)); 
}

The onPause() and onResume() methods are called when the application is brought to the background and into the foreground again. onPause()onResume()方法在应用程序进入后台并再次进入前台时被调用。 However, they are also called when the application is started for the first time and before it is killed.但是,它们也会在应用程序第一次启动和终止之前被调用。

There isn't any direct approach to get the application status while in the background or foreground, but even I have faced this issue and found the solution with onWindowFocusChanged and onStop .没有任何直接的方法可以在后台或前台获取应用程序状态,但即使我也遇到过这个问题,并通过onWindowFocusChangedonStop找到了解决方案。 For more details please check this.有关更多详细信息,请检查此。

Happy coding!!快乐编码!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM