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
  • 11,166
  • 2
  • 35
  • 45
Ian M
  • 567
  • 8
  • 33
  • `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

4

try :

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

it is working for me.

Eliott Roynette
  • 716
  • 8
  • 21
  • 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
  • 18,838
  • 12
  • 85
  • 135
0

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

adboco
  • 2,840
  • 1
  • 21
  • 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
  • 678
  • 5
  • 20
  • 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
  • 50,076
  • 60
  • 203
  • 339