Reputation: 17
I'm using a broadcastreceiver called MyTimeReceiver to display a toast at intervals every hour (every 10 seconds for testing). My problem is that the toast is not displaying.
Here is my code snipped from the main activity file (SafeDrive3Activity):
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
//add 10 seconds to calendar object
cal.add(Calendar.SECOND, 10);
mAlarmSender = PendingIntent.getBroadcast(SafeDrive3Activity.this,
0, new Intent(SafeDrive3Activity.this, MyTimeReceiver.class), 0);
// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,
SystemClock.elapsedRealtime(),mAlarmSender);
broadcastreceiver class:
public class MyTimeReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"HOUNOTIFICATION", Toast.LENGTH_LONG).show();
}
}
Please help this has been driving me mad for hours.
Upvotes: 0
Views: 303
Reputation: 9072
Try this for displaing toast every 10 sec.
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000*10, mAlarmSender));
Upvotes: 0
Reputation: 8176
This one is all over the place. You create a Calendar
you never use, you tell the AlarmManager
to use the RTC_WAKEUP
then use the elapsedRealtime()
time source :).
Try this:
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), mAlarmSender);
Upvotes: 1