0

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);

    }

}
Community
  • 1
  • 1
Amani Swann
  • 37
  • 4
  • 10
  • 1
    http://stackoverflow.com/questions/6446221/get-context-in-a-service. and this http://developer.android.com/reference/android/app/Service.html and this http://developer.android.com/reference/android/content/Context.html – Raghunandan Jun 14 '13 at 14:41
  • Service IS a Context... – shkschneider Jun 14 '13 at 14:43

1 Answers1

1

Service is a Context, so try this code:

AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

PendingIntent sender = PendingIntent.getBroadcast(this,
Dimmerg
  • 2,113
  • 1
  • 13
  • 14
  • Thank you! That resolved the issue regarding the error "context cannot be resolved." however I'm still getting "context cannot be resolved to a variable" on the line: PendingIntent sender = PendingIntent.getBroadcast(context, Integer.parseInt(Long.toString(id)), intent, 0); – Amani Swann Jun 14 '13 at 15:21
  • Please, note on my first answer: your service is a context, so you can use "this" for all functions where you should pass context. PendingIntent sender = PendingIntent.getBroadcast(this, Integer.parseInt(Long.toString(id)), intent, 0); – Dimmerg Jun 14 '13 at 15:26
  • Ahhhhh... thank you! (Also - I have one last question/error in my source: id cannot be resolved to a variable - I do not see it declared in the SO example I'm attempting to use - any suggestions?) – Amani Swann Jun 14 '13 at 15:31
  • It's "requestCode", you can define it by yourself: http://developer.android.com/reference/android/app/PendingIntent.html#getBroadcast(android.content.Context, int, android.content.Intent, int) But for more details about this code you should googling again) – Dimmerg Jun 14 '13 at 15:39
  • I just read the article... I'm still a bit lost on what I should do to declare/define the ID variable – Amani Swann Jun 14 '13 at 15:54
  • private static final int IntentID = 10; PendingIntent sender = PendingIntent.getBroadcast(this, IntentID, intent, 0); – Dimmerg Jun 15 '13 at 05:30