0

I have an dashcam which automatically starts and stops recording based on charging (start on charging and stop when it stops charging)

So I have something like this at the BroadcastReceiver.onReceive

Intent.ACTION_POWER_CONNECTED -> {
    startRecording()
}
Intent.ACTION_POWER_DISCONNECTED -> {
    stopRecording()
}

Some users reported that recording stops when their device becomes fully charged though it is still plugged to a power source via USB.

So it seems BatteryManager sends the event that the power is not connected.

What can I do in this case to know for sure that it is still plugged to a source power?

Mb should I use the following solution?

Intent.ACTION_BATTERY_CHANGED -> {
    val plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0
    // conditionally it looks like this: if (plugged) startRec() else stopRec()
}
user924
  • 8,146
  • 7
  • 57
  • 139
  • check this out https://stackoverflow.com/questions/6217692/detecting-the-device-being-plugged-in – Rob Nov 01 '22 at 20:56
  • @Rob what exactly? it's just the same code I have here – user924 Nov 01 '22 at 20:57
  • you are using 0 as default value for this line val plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) and may be this will return 0 for any not appropriate case - but in this answer there has been used -1 for default value - and also I think this example the code is more detailed - compare with your original code - may be you have small invisible(for the first look) issue – Rob Nov 01 '22 at 21:08
  • @Rob it's just a number for default value, it doesn't matter if it's 0 or -1... that answer uses `EXTRA_PLUGGED` as well, also one commentator said that this is not good because when it's fully charged then it says that it's not plugged to a power source – user924 Nov 01 '22 at 21:40

0 Answers0