0

I'm trying out alarms and have hit a wall. I don't think my alarm is setting up properly because I never get a confirmation after the alarm is supposed to go off. Here's how I call on the alarm manager:

long alarmtime=new GregorianCalendar().getTimeInMillis()+10*1000;//run after 10 seconds
Intent i = new Intent(this, AlarmReceiver.class);
        PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
        AlarmManager alarmman = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmman.cancel(pi); // cancel any existing alarms

        alarmman.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                alarmtime, 10 * 1000, pi);//run every 10 seconds

And here's my AlarmReceiver.java:

public class AlarmReceiver extends BroadcastReceiver {
     @Override
     public void onReceive(Context context, Intent intent) {
         Toast.makeText(context, "TEST",    Toast.LENGTH_LONG).show();
     }

}

However, the TEST text does not appear, and I can't figure out why.

Bonhomme
  • 33
  • 2
  • 10

2 Answers2

0

Since you are using the AlarmManager.ELAPSED_REALTIME_WAKEUP argument, your initial alarm-time should be base on the elapsed real time of the device:

long alarmtime = SystemClock.elapsedRealtime() + 10 * 1000;
alarmman.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, alarmtime, 10 * 1000, pi);

(see this link).

For a BroadcastReceiver it's probably PendingIntent.getBroadcast() instead of PendingIntent.getService(). You are also cancelling the alarm, just update your PendingIntent like this and try not cancelling before:

PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

Read the documentation thoroughly for further information.

Community
  • 1
  • 1
Blacklight
  • 3,809
  • 2
  • 33
  • 39
  • Just tried it, and it's not working either. I'm not even getting any error messages. – Bonhomme Jul 17 '14 at 06:30
  • See my edit regarding the cancelling. If this doesn't work try registering the receiver in your manifest, but other than that everything else is correct.. try not triggering a toast, debug instead or print to the logcat to be sure. – Blacklight Jul 17 '14 at 06:42
  • I used log.w in alarmreceiver.java but it doesn't show up in logcat, which proves that it isn't opening. – Bonhomme Jul 17 '14 at 07:06
  • Based on your code, this is all help I can give you, it should work this way. Like I said read the docs thoroughly, see if you missed anything in your code, pay attention to when you are triggering this alarm (app lifecycle), debug to see if you reach that code etc. Also you could try not using elapsed realtime but the normal wake-up based on your current time! – Blacklight Jul 17 '14 at 07:14
  • Thanks. I'll let you know if I figure it out somehow :p – Bonhomme Jul 17 '14 at 07:15
  • Turns out in my manifest I typed in instead of ............BRB GONNA GO KILL MYSELF IN THE MOST BRUTAL WAY POSSIBLE – Bonhomme Jul 17 '14 at 08:13
  • Since you had to errors in your code anyway it wasn't for nothing. ;) Good to hear it's working now. – Blacklight Jul 17 '14 at 08:43
0

Make sure that the BroadcastReceiver is being called by doing a System.out.println("TEST"); instead of Toast. If you are able to see that in your logcat, then the problem probably is that you need to run the Toast in UI thread.

Ruraj
  • 467
  • 2
  • 8
  • 13