1

I have a service that collect data, and every hours the service need to send this data to my server. I know their is plenty of solutions available to schedule task, but in my case what will be the most efficient regarding battery consumption and resource utilization ?

also my service can be stopped, and when my service is stopped then it's must not send anymore any data (so alarmmanager seam to be not the best way because it's will restart my service as far as i know)

k3b
  • 14,517
  • 7
  • 53
  • 85
zeus
  • 12,173
  • 9
  • 63
  • 184
  • Any sort of "task" you implement will need to be explicitly stopped if your service stops just like alarm manager would but you should look into JobScheduler – tyczj Jul 12 '17 at 14:46

2 Answers2

1

To schedule via the AlaramManager is perfectly fine since you can also cancel the scheduled task.

It could look like this

 AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
 Intent intent = new Intent(getApplicationContext(), YourService.class);
 alarm.set(
    AlarmManager.RTC_WAKEUP,
    System.currentTimeMillis() + DateUtils.HOUR_IN_MILLIS,
    PendingIntent.getService(getApplicationContext(), 11, intent, PendingIntent.FLAG_UPDATE_CURRENT)
 );

and to cancel it could be done like this

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent myIntent = new Intent(getApplicationContext(), YourService.class);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 11, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(pendingIntent);

The 11 is the identifier for the PendingIntent in which you can get access to it.

Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
  • @loki Yes, read more here about [Services](https://developer.android.com/reference/android/app/Service.html#ServiceLifecycle) – Murat Karagöz Jul 12 '17 at 15:06
  • what about Firebase JobDispatcher – AKASH WANGALWAR Jul 12 '17 at 15:17
  • @MuratK. do i need to do always first alarmManager.cancel following by alarm.set to be sure that their is not pending alarm ? – zeus Jul 12 '17 at 15:38
  • @loki Yes, that's how I handle it aswell. – Murat Karagöz Jul 12 '17 at 15:39
  • but when i read https://developer.android.com/training/scheduling/alarms.html it's say: For timing operations that are guaranteed to occur during the lifetime of your application, instead consider using the Handler class in conjunction with Timer and Thread. This approach gives Android better control over system resources – zeus Jul 12 '17 at 15:44
  • @loki That's for short lifetime threads. Or will your App be open 24/7? You talked about power consumption and this is a good approach. If this answer helped you please mark it as that. If you have more questions about services, open a new question. – Murat Karagöz Jul 12 '17 at 15:47
1

Using the alarm manager alone is not enough.

you should send your data when the networkconnection is already active in use because shifting the WiFi or 3G/4G radios from "idle/not in use" to "data send/receive" is very energy expensive. If the device is already sending/receiving data you do not have the additional energy cost to activate the radio.

You can register a broadcast receiver that informs your app about the current network state.

For more details see

Your transfer strategy would be a mixture of

  • alarmmanager
  • is the device is currently charging (so battery is not not important at this moment)
  • is the radio currently active
k3b
  • 14,517
  • 7
  • 53
  • 85
  • i fully agree with you, but how to do all of this ? any sample ? – zeus Jul 12 '17 at 16:10
  • see https://developer.android.com/training/efficient-downloads/efficient-network-access.html – k3b Jul 12 '17 at 16:16
  • i read the doc, but nothing explain me what to do exactly :( it's just say what you say (and what i know already, purpose of this question), what i need to know is how to implement it :( how to efficiently send data via network with alarmmanager. for example it's evendent that if when the alam fired their is no network connection, better to wait a network connection, but how to do all of this – zeus Jul 12 '17 at 16:34
  • for exemple, just simple think like did i need to setup the alarm via RTC or via RTC_WAKEUP (does the network connection need rct_wakup?) – zeus Jul 12 '17 at 16:41