7

I'm testing AlarmManager to use in my app, and it is firing my Broadcast Receiver immediately when I want it to fire after 1 minute. The code is below:

public class SetMealTimersActivity extends Activity {
    PendingIntent pi;
    BroadcastReceiver br;
    AlarmManager am;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_meal_timers);

        br = new BroadcastReceiver() {
            @Override
            public void onReceive(Context c, Intent i) {
                Toast.makeText(c, "Ready to Go!", Toast.LENGTH_LONG).show();
            }
        };
        registerReceiver(br, new IntentFilter("com.ian.mealtimer"));
        pi = PendingIntent.getBroadcast(this, 0, new Intent(
                "com.ian.mealtimer"), 0);
        am = (AlarmManager) (this.getSystemService(Context.ALARM_SERVICE));         
        am.set( AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 
                60 * 1000, pi );
    }
nikis
  • 10,991
  • 2
  • 33
  • 45
Ian M
  • 537
  • 7
  • 29
  • `Intent intentAlarm = new Intent(this, AlarmReciever.class);AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); //set the alarm for particular time alarmManager.set(AlarmManager.RTC_WAKEUP,time, PendingIntent.getBroadcast(this,1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT)); where time = 60*1000` – Aniruddha Jun 02 '14 at 12:37

5 Answers5

3

try :

  am.set(AlarmManager.RTC_WAKEUP, 
      Calendar.getInstance().getTimeInMillis()+60*1000, pendingIntent);

it is working for me.

Eliott Roynette
  • 696
  • 7
  • 20
  • I just substituted your code for the last line in my code but it is still not working for me - the Broadcast Receiver is firing immediately ??? – Ian M Jun 02 '14 at 12:55
2

If using an exact alarm, make sure it's time is in the future. Otherwise it will fire immediately.

Daniel Wilson
  • 17,944
  • 11
  • 82
  • 117
0

Try changing SystemClock.elapsedRealtime() to System.currentTimeMillis() and AlarmManager.ELAPSED_REALTIME_WAKEUP to AlarmManager.RTC_WAKEUP.

adboco
  • 2,680
  • 1
  • 20
  • 21
0

Try to use AlarmManager.setExact(int, long, PendingIntent) if you use Android API > 18 or compile with API < 19, because the time management for this methods changed with API 19. Maybe that helps. Read the documentation for more information.

KCD
  • 658
  • 5
  • 18
  • setExact (using the same parameters as before) gives exactly the same result - immediate firing of the broadcast receiver :0( – Ian M Jun 02 '14 at 13:48
  • Did you try to debug your code? Is the BroadcastReceiver really called after AlarmManager.set ? Or is it called immediately after registration of the receiver? – KCD Jun 02 '14 at 14:03
  • Apologies - my BroadcastReceiver was not actually being called, it was simply that the Activity in which it was defined was being displayed. Sorry for the confusion – Ian M Jun 02 '14 at 16:08
0

make id for pendingIntent as this

pendingIntent = PendingIntent.getActivity(this, 999123266,
    intent, PendingIntent.FLAG_UPDATE_CURRENT);

All example

public void setAlarm_sat(int dayOfWeek1) {
    cal1.set(Calendar.DAY_OF_WEEK, dayOfWeek);
    Intent intent = new Intent(this, RemmemberActivity.class);
    pendingIntent = PendingIntent.getActivity(this, 999123266,
        intent, PendingIntent.FLAG_UPDATE_CURRENT);

    Long alarmTime = cal1.getTimeInMillis();
    AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, 
                    alarmTime,7*24 * 60 * 60 * 1000, 
                    pendingIntent);
    // am.set(AlarmManager.RTC, cal1.getTimeInMillis(), pendingIntent);
}
JJD
  • 47,369
  • 53
  • 194
  • 328