0

i have a notification that display the battery level. The notification is in a service because even if the app is closed the service still run and it can display the percentage of battery.. I've notice the battery level go down faster when the notification is active.. Is there a way to resolve this problem?

Edit: onCreate of service

@Override
    public void onCreate(){
        super.onCreate();
        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        new IntentFilter(Intent.ACTION_BATTERY_CHANGED);    
    }

and in the manifest i wrote this too ACTION_BATTERY_CHANGED

David_D
  • 1,404
  • 4
  • 31
  • 65
  • I think that's normal since you are continually running service on background and it will eat pretty significant battery life. By the way why would display the battery level using notification? Does the battery status is isn't enough? – Ariel Magbanua Aug 21 '13 at 07:38
  • Because my application shows (inside the MainActivity i mean) some battery informations and in the settings you can activate a permanent notification with battery level and temperature. It's what other applications do :) But yeah, eat battery. It's a contraddiction! It's a battery saver app! So you think there isn't another way? – David_D Aug 21 '13 at 07:42
  • This is the app https://play.google.com/store/apps/details?id=com.dd.batterystats – David_D Aug 21 '13 at 07:43
  • How frequently are you querying the BatteryManager? I would stick to the advised way, see link: http://developer.android.com/training/monitoring-device-state/battery-monitoring.html#CurrentLevel – M.Bennett Aug 21 '13 at 07:44
  • Well every time the notification receive the broadcastreceiver. – David_D Aug 21 '13 at 07:50

1 Answers1

0

Ideally, you'd want to only update your views when the battery state changes. Have you tried monitoring this broadcast action rather than polling for battery level?

http://developer.android.com/reference/android/content/Intent.html#ACTION_BATTERY_CHANGED

That way you only have something perform an action when the state changes, rather than all the time in the background.

MattMatt
  • 2,242
  • 33
  • 30
  • i already use the battery changed intent. But seems that update not always when battery change level but also (i don't know why) every 10 seconds. But.. I never wrote something like this. – David_D Aug 21 '13 at 07:54
  • 1
    It looks like the battery changed event occurs periodically, regardless of battery state changes. There's a potential solution here that uses polling as a workaround: http://stackoverflow.com/questions/7624882/action-battery-changed-firing-like-crazy – MattMatt Aug 21 '13 at 08:03
  • So do you think it's better decide an interval to update for example? – David_D Aug 21 '13 at 08:08
  • could be a problem if i write the Broadcast `ACTION_BATTERY_CHANGED` in the manifest and also in the java service? – David_D Aug 21 '13 at 10:15
  • 1
    I'd say so yes, you know for sure the interval then, which will make life easier when you get to market. From some reading around, it appears that this update interval varies by device. Yay, fragmentation... :-/ – MattMatt Aug 21 '13 at 12:02
  • I've edit the first post..maybe if i cancel the Intent in the onCreate something change. – David_D Aug 21 '13 at 12:27