TruMan1
TruMan1

Reputation: 36098

How to execute code when device is charging and reboot?

I need to call a JSON web service over the web to download and update the device database. It doesn't need to be real time like push notifications, so 1 or 2 days is fine to update the device.

Is there a way activate my code only when the device is charging and on reboot? I figure this would cover my cases for users charging a dead phone. Maybe I need to add another case for when internet is available also?

I am thinking I need a BroadcastReciver or a Service, but I am not sure which one. Do I need to even use AlarmManager since this is not on a schedule?

Upvotes: 0

Views: 137

Answers (1)

Shweta
Shweta

Reputation: 172

You can use a different approach.

  1. You can have a small part of the code running as a service in the background http://examples.javacodegeeks.com/android/core/service/android-service-example/
  2. You can poll the isCharging for Android. As soon as this happens, the activity is started. https://developer.android.com/training/monitoring-device-state/battery-monitoring.html

Upvotes: 1

Related Questions