-2

I'm attempting to set a reoccurring alarm example using code taken from the following SO example:

Implementing an alarm every 5 days, code correct?

However I'm getting an error stating "context cannot be resolved to a variable" on the line:

PendingIntent pi = PendingIntent.getBroadcast(context, 0, Aintent, 0);

Any suggestions?

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) {
        // method to check if an alarm must be fired today

        String alarm = Context.ALARM_SERVICE;
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Calendar calendar = Calendar.getInstance();
         calendar = Calendar.getInstance();
         calendar.set(Calendar.DAY_OF_WEEK, 5);
         calendar.set(Calendar.HOUR_OF_DAY, 23);
         calendar.set(Calendar.MINUTE, 0);
         calendar.set(Calendar.SECOND, 0);

         Intent Aintent = new Intent("REFRESH_THIS");

         PendingIntent pi = PendingIntent.getBroadcast(context, 0, Aintent, 0);
        am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis() , AlarmManager.INTERVAL_DAY, pi);


        // reschedule 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 Answers1

2
PendingIntent pi = PendingIntent.getBroadcast(context, 0, Aintent, 0);

The problem is in this line. You are using a variable context without declaring it. Use like

PendingIntent pi = PendingIntent.getBroadcast(this, 0, Aintent, 0);

Also you need to return an int from onStartCommand

use return START_STICKY;

stinepike
  • 54,068
  • 14
  • 92
  • 112