Reputation: 3
I have an app that calls a Service
with the AlarmManager
on the main activity and inside that Service
makes another alarm to call itself moments after. The alarm in the main activity works fine but the alarm in the Service
doesn't set off. Anyone know why?
This is the alarm in my main activity:
Intent i = new Intent(this, DailyLogsService.class);
Calendar time=Calendar.getInstance();
time.set(Calendar.HOUR_OF_DAY,13);
time.set(Calendar.MINUTE,15);
time.set(Calendar.SECOND,0);
AlarmManager alarmM=(AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pending= PendingIntent.getService(getApplicationContext(),0,i,0);
//alarmM.cancel(pending);
alarmM.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pending);
Toast.makeText(this, "STARTING", Toast.LENGTH_LONG).show();
and this is the alarm on my service
@Override
public void onCreate() {
Intent i = new Intent(getBaseContext(), DailyLogsService.class);
Calendar time=Calendar.getInstance();
time.set(Calendar.HOUR_OF_DAY, 13);
time.set(Calendar.MINUTE,30);
time.set(Calendar.SECOND,0);
AlarmManager alarmM=(AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pending= PendingIntent.getService(getApplicationContext(),0,i,0);
alarmM.setExact(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pending);
}
Upvotes: 0
Views: 280
Reputation: 6828
Move your alarm code in onCreate
of the Service
to onStartCommand
method.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Intent i = new Intent(getBaseContext(), DailyLogsService.class);
Calendar time=Calendar.getInstance();
time.set(Calendar.HOUR_OF_DAY, 13);
time.set(Calendar.MINUTE,30);
time.set(Calendar.SECOND,0);
AlarmManager alarmM=(AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pending= PendingIntent.getService(getApplicationContext(),0,i,0);
alarmM.setExact(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pending);
return START_NON_STICKY;
}
Upvotes: 1