I'm attempting to use a method to schedule monthly alarms described here:
How to implement yearly and monthly repeating alarms?
However I'm getting two errors on the lines:
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
and
PendingIntent sender = PendingIntent.getBroadcast(context,
stating "context cannot be resolved" and "context cannot be resolved to a variable"
...any suggestions on how this can be resolved?
public class Alarm extends Service {
// compat to support older devices
@Override
public void onStart(Intent intent, int startId) {
onStartCommand(intent, 0, startId);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// your method to check if an alarm must be fired today
PendingIntent sender = PendingIntent.getBroadcast(context,
Integer.parseInt(Long.toString(id)), intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(calendar.getTimeInMillis());
calendar.add(Calendar.SECOND, 30);
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
calendar.add(Calendar.MONTH, 1);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
// reschedule me to check again tomorrow
Intent serviceIntent = new Intent(Alarm.this, Alarm.class);
PendingIntent restartServiceIntent = PendingIntent.getService(
Alarm.this, 0, serviceIntent, 0);
AlarmManager alarms = (AlarmManager) getSystemService(ALARM_SERVICE);
// cancel previous alarm
alarms.cancel(restartServiceIntent);
// schedule alarm for today + 1 day
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
// schedule the alarm
alarms.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
restartServiceIntent);
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
return super.onUnbind(intent);
}
}
Service is a Context, so try this code: