0

I need to run an application to run four times a day at specific time. I wrote this code and someone told me this is a bad way. I don't know if there is better ways to do that.

Intent myIntent = new Intent(this,MyService.class);
    pendingIntent = PendingIntent.getService(this, 0,myIntent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 60);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 60*1000, pendingIntent);

It keeps calling the MyService every minute. Is there any way to run service at specific time?

stefan
  • 10,215
  • 4
  • 49
  • 90

1 Answers1

1

Waking up the phone every minute is pretty battery intense - if you only need to run it 4 times a day, you could simply schedule it at 6 hour intervals. You may also want to consider something other than RTC_WAKEUP (e.g. RTC) so that you aren't unnecessarily waking the phone up when it is sleeping. If you don't really care when it goes off exactly then there is little reason to explicitly wakeup the device.

The way apps do this is to watch for the change, then they poll their server comparing their update version to the newest available on the server and if the server has a newer update they pull it. You can get an idea of how to watch for network connection changes here: http://viralpatel.net/blogs/android-internet-connection-status-network-change/

Gabe
  • 86
  • 1
  • 6