I am trying to get informataion from battery. So i've created BroadcastReceiver, to read this values in background. I've got the problem with get isCharging state from battery. I've got always false. I am trying to use this code:
public class AlertReceiver extends BroadcastReceiver {
@Overrid
public void onReceive(Context context, Intent intent) {
BatteryManager bm = (BatteryManager) context.getSystemService(BATTERY_SERVICE);
assert bm != null;
int battery_level = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
}
}
Manifest
<receiver android:name=".service.AlertReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
<action android:name="BackgroundProcess"/>
</intent-filter>
</receiver>