How can I detect when and how long the charger is plugged? I have class which extends BroadcastReceiver and override onReceive. I need to check start and end charging and duration time, but I don't know where to start. Could you please help me?
-
See this, maybe can help you: http://stackoverflow.com/questions/17440729/how-to-identify-the-connection-to-charger-port – Fustigador May 10 '14 at 14:18
-
Thank you for your response:) I am able to check whether the battery is charging but I have problem with calculation how long – user3590445 May 10 '14 at 14:30
-
You can capture date when you receive the charger plugin intent and again with the battery full intent, get the difference in miliseconds, and get the time from there. – Fustigador May 10 '14 at 17:13
1 Answers
I have class which extends BroadcastReceiver and override onReceive. I need to check start and end charging and duration time, but I don't know where to start.
At first you need to create correct BroadcastReceiver
that will listen for Battery charging changes. You can create it statically via Manifest:
<receiver android:name=".BatteryReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>
Then your Java class must match it's name:
public class BatteryReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// do your stuff
}
}
Or you can create BoadcastReceiver dynamically and you can bound it into Service or Activity (it depends on your needs):
private void registerChargingReceiver() {
if (intentFilter == null) {
intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);
intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
}
if (receiver == null) {
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent i) {
// changer is connected
if (i.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
// do proper actions
}
// changer is disconneted
if (i.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)) {
// do proper actions
}
}
};
}
// registering receiver
registerReceiver(receiver, intentFilter);
}
How can I detect when and how long the charger is plugged?
It can be achieved in more possible ways. For sure you need to save somewhere time when charger is connected and disconnected and then substract times:
long chargingTime = endChargingTime - startChargingTime;
And you can use SharedPreferences where you'll save your times (pseudo-code):
if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
// remove time when charger was diconnected (last before)
prefs.edit().remove("chargingEndTime");
// save time when charged is connected
prefs.edit().putLong("chargingStartTime", System.currentTimeMillis());
prefs.edit().commit();
}
if (intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)) {
// save time when charger is disconnected
prefs.putLong("chargingEndTime", System.currentTimeMillis()).commit();
}
Hope it'll help to solve your problem you're facing.

- 1
- 1

- 33,374
- 10
- 68
- 106
-
Thank you! Could you tell me, my public class BatteryReceiver extends BroadcastReceiver is inner, so must be static? Otherwise, application is stoped – user3590445 May 10 '14 at 14:48
-
@user3590445 why is inner? why cannot by normal in single file? – Simon Dorociak May 10 '14 at 14:52
-
Ok:) But I don't understand how can I create sharedpreferences in class BatteryReceiver, but I need it for use in onReceive, right? – user3590445 May 10 '14 at 15:06
-
@user3590445 Create broadcastReiver dynamically how in example. – Simon Dorociak May 10 '14 at 15:21