Android: do system drawables have static IDs?
I asked this question about using the system-drawables in an Android-app some time ago:
Android: access "system"-drawablesI finally found out, that there actually is a way to access those drawables, but especially concerning the battery-icons, there's a catch: it doesn't work as expected. :)
As e.g. trying to acccess the "empty battery"-icon with R.drawable.stat_sys_battery_0
did not work, I found this: intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL, -1)
which actually does return the integer-ID of "stat_sys_battery_0" and let's me access it, but the documentation states it should return an "icon indicating the current battery state". But actually, I'm always getting the icon for the empty battery no matter what the battery-level is. This happens in the emulator as well as on my phone.
What I tried:
The drawable-ID I get fromgetIntExtra
is "17302150" (in the emulator and on the phone). Using this ID, I get the "empty battery"-icon. I played around with the IDs, and found some other battery icons by using random IDs, so my thought was to just try and find the IDs of all needed icons and put them in my code statically.
Now, two questions:
1. Is it safe to use static IDs in an app or开发者_JAVA百科 are the drawable-IDs subject to change throughout SDK-levels or on different devices? 2. As that's just a workaround: Does anybody know why I keep getting the same drawable fromgetIntExtra
?
Any help is appreciated.
Is it safe to use static IDs in an app or are the drawable-IDs subject to change throughout SDK-levels or on different devices?
I would not rely on them. Moreover, I would not even rely on the icons. Have your own battery level icons in your app, and use them. Device manufacturers can change the icons, meaning they may or may not look (or be sized) the way you expect.
Does anybody know why I keep getting the same drawable from getIntExtra?
Possibly because the icon has nothing to do with battery level. Looking at the source code, it will either give you:
stat_sys_battery_charge
if the battery is chargingstat_sys_battery
if the battery is discharging, not charging, or fullstat_sys_battery_unknown
otherwise
精彩评论