Android examples for Hardware:Battery
is Enough Battery
//package com.java2s; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.os.BatteryManager; import android.preference.PreferenceManager; public class Main { public static Boolean isEnoughBattery(Context context) { SharedPreferences pref = PreferenceManager .getDefaultSharedPreferences(context); Float acceptableBatteryLevel = Float.parseFloat(pref.getString( "pref_battery_level", "40.0f")); Float batteryLevel = getBatteryLevel(context); return (batteryLevel >= acceptableBatteryLevel || isCharging(context)); }//from w ww.ja v a2 s . co m private static Float getBatteryLevel(Context context) { Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); // Error checking that probably isn't needed but I added just in case. if (level == -1 || scale == -1) { return 50.0f; } return ((float) level / (float) scale) * 100.0f; } private static Boolean isCharging(Context context) { IntentFilter ifilter = new IntentFilter( Intent.ACTION_BATTERY_CHANGED); Intent batteryStatus = context.registerReceiver(null, ifilter); int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1); return status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL; } }