Monday, 7 September 2015

Android battery infor monitoring


First you can get all code from there:

http://developer.android.com/training/monitoring-device-state/battery-monitoring.html


I just put some addition information for you basing on my experience :

1.  you can't register "BATTERY_CHANGED" in AndroidManifest.xml.
2. "BATTERY_LOW" will be at least boardcasted 2 times if you test the device from fully charged to no battery.  20% once and 10% once.
3. "BATTERY_OKAY" will be boardcasted once you battery is back from low battery. and if you battery is low, when you charge it, it will boardcast this event as well if more than 10%.

4. "IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);" is will give you all battery info including battery percentage and charging status. But it doesn't work after "ACTION_POWER_CONNECTED", I think it is because the system still preparing and setting. so maybe a delay can solve this.



Amendment: for point4, delay doesn't solve the issue:

private void powerConnected(final Context context, final String action) {
    TimerTask task = new TimerTask(){
        public void run(){
            getBatteryStatus(context, action);
        }
    };
    Timer timer = new Timer();
    timer.schedule(task,15000);
}







No comments:

Post a comment