0

I asked this question about using the system-drawables in an Android-app some time ago:
Android: access "system"-drawables

I 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 from getIntExtra 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 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 from getIntExtra?

Any help is appreciated.

Community
  • 1
  • 1
Select0r
  • 12,234
  • 11
  • 45
  • 68

1 Answers1

3

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 charging
  • stat_sys_battery if the battery is discharging, not charging, or full
  • stat_sys_battery_unknown otherwise
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I was hoping to be able to use system icons, because using own icons will bloat the app and I like to keep it as small as possible. I was mistaking the statement "indicating the current battery state" for the "battery level" (because I thought that made more sense.) I still think it's strange that a) You can access system icons but not by their name, and 2. that "battery state" will give me the "battery empty"-icon when status is "full" or just "discharging". But anyway, thanks for the insight! – Select0r Feb 12 '11 at 23:33