3

I am making an application which allows us to send sms to our contacts automatically, i.e the user interface allows us to insert the time and sms content which we have to send. Now how do I check that it is time to send sms even if the application is closed.

I think services are used for theses tasks?

Mohammad Areeb Siddiqui
  • 9,795
  • 14
  • 71
  • 113
mdanishs
  • 1,996
  • 8
  • 24
  • 50

2 Answers2

2
private void setRecurringAlarm(Context context) {

    Calendar updateTime = Calendar.getInstance();
    updateTime.setTimeZone(TimeZone.getTimeZone("GMT"));
    updateTime.set(Calendar.HOUR_OF_DAY, 11);
    updateTime.set(Calendar.MINUTE, 45);

    Intent downloader = new Intent(context, AlarmReceiver.class);
    PendingIntent recurringDownload = PendingIntent.getBroadcast(context, 0, downloader, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager alarms = (AlarmManager) getActivity().getSystemService(
            Context.ALARM_SERVICE);
    alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP,
            updateTime.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, recurringDownload);
}

This Alaram Manager will invoke AlarmReciever at scheduled time. Do it this way.

You can find more details here: Scheduling Task

regards, Aqif Hamid

Aqif Hamid
  • 3,511
  • 4
  • 25
  • 38
1

Use an alarm manager for this, set up a broadcast receiver that goes when the "alarm" goes of and then make it send the messages, have a look here for the alarm manager Alarm Manager Example

Community
  • 1
  • 1
FabianCook
  • 20,269
  • 16
  • 67
  • 115