Error resetting alarm with AlarmManager shortly after canceling alarm

0

I have an alarm set to wake up at a certain time and repeat every 5 minutes. But after a few repetitions, I automatically set the cancellation of that, that is, I call the method to cancel " cancelAlarm(); " and then call the method setNewAlarm();

But when it arrives in the second method, it returns me the following error:

E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-11525
java.lang.IllegalStateException: System services not available to Activities before onCreate()

I appreciated an illustration of something I was not supposed to see. Thank you. Below the code:

File1.java

File2 f2 = new File2();
File3 f3 = new File3();
if (lunchHour == hr && lunchMinute == minut && secs == 0) {  
    f3.cancelAlarm(context);
    f2.setNewAlarm();
} else {
    if (finishWorkHour == hr && finishWorkMinute == minut && secs == 0) { 
        f3.cancelAlarm(context);
        f2.setNewAlarm();
    }
}

File2.java

public void setNewAlarm() {

    int afterLunchHour = 17;
    int afterLunchMinute = 58;

    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent myIntent = new Intent(SentadoAlmoco.this, MyReceiver.class);
    PendingIntent pi = PendingIntent.getBroadcast(SentadoAlmoco.this, 0, myIntent, 0);

    //Set the alarm to start at a specific time
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(System.currentTimeMillis());
    cal.set(Calendar.HOUR_OF_DAY, afterLunchHour);
    cal.set(Calendar.MINUTE, afterLunchMinute);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);

    am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1000 * 60 * 5, pi);
}
    
asked by anonymous 20.01.2015 / 18:07

0 answers