Reputation: 23596
Here i am going to use the alarm service to play the alarm at every 30 minutes. Right now i have set it to play it at every 10 second from the Every start.
Here is the Code:
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_layout);
Intent myIntent = new Intent(SettingsActivity.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(SettingsActivity.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10*1000, pendingIntent);
}
Now the Problem is, I want to start the alarm from the 12:30 not from the time application start and it should repeatedly play at evert 30 minutes. like 1:00, 1:30, 2:00 . . . etc
So what changes i have to do in my code ?
Upvotes: 1
Views: 11323
Reputation: 23
For repeating every 30 minutes you will need to add this to your code:
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*30, pendingIntent); //millisecs*seconds*minutes
But I haven't figure out how to start at specific time yet.
Upvotes: 1
Reputation: 9668
A naive approach would be:
Make sure you catch notify interruptions to the tread gracefully, that is the key to aborting the timer.
Hope this was helpful.
Upvotes: 1
Reputation: 35661
Set your initial alarm time for 12:30 using the Set method.
When the alarm fires, then set up your next alarm time and keep doing that until you don't want the alarm any more.
You don't need a service to do such a simple task. AlarmManager is more than capable of handling this.
Upvotes: 1