How do you change the nav-bar background (not just color), even when not in foreground?

梦想与她 提交于 2019-12-03 07:11:47

问题


Background

Somehow the app "Navbar Apps" allows to customize the background of the nav bar, even if it's not in the foreground, and even not just a simple color.

The problem

I thought it's only possible to change the color of it, and only when the app is in the foreground...

I've seen it on some custom roms, even having special effects when music plays, but I didn't know it's possible to customize it even without a custom rom (or root).

The questions

  1. Is it a hack? How do they change, not just the color of the navigation bar, but even set a background image for it (including dynamic one, for battery status) ?

  2. Is it also possible to change other system bars, like the notification bar?

  3. How does it check which app is in the foreground (needed for deciding when to change the color, based on current app, probably)? Is it a new API ? I thought that the API for getting the foreground activity got deprecated and now doesn't help in any way...


回答1:


The basic setup is quite easy. Making it into an app will take some work.

You'll need to use the Accessibility APIs, along with WindowManager#addView(...).

Is it a hack?

I can't say I like the idea, but its not exactly a hack.

How do they change, not just the color of the navigation bar, but even set a background image for it (including dynamic one, for battery status) ?

Since we're adding a View (or ViewGroup), we have a lot more control.

Is it also possible to change other system bars, like the notification bar?

I'll look into this.

How does it check which app is in the foreground (needed for deciding when to change the color, based on current app, probably)?

You can use Accessibility APIs to listen for Window level changes (AccessibilityEvent) - this will give you the packageName. Use it as you like.

I'll provide you with some pointers:

Manifest:

  • permission: SYSTEM_ALERT_WINDOW
  • accessibility service declaration: <service />

Accessibilityservice

  • Inflate, initialize & add your layout in: onServiceConnected()
  • I used TYPE_SYSTEM_OVERLAY with WindowManager#addView(...)
  • Listen for AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
  • Override onAccessibilityEvent(AccessibilityEvent); packageName will be available here

Outcome

An example project is hosted here: Link.



来源:https://stackoverflow.com/questions/39383183/how-do-you-change-the-nav-bar-background-not-just-color-even-when-not-in-fore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!