17

Hi I am currently working with AlarmManager. I have written a code given below. As per code the AlarmManager should be triggered after 10 Sec, but here in my code the alarm manager triggers immediately. Please help.

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP;
        long timeOrLengthofWait = 10000;
        Intent intentToFire = new Intent(this, AlarmReciever.class);
        PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intentToFire, 0);
        alarmManager.set(alarmType, timeOrLengthofWait, alarmIntent);
    }
}

And My AlarmReciever Class

public class AlarmReciever extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        String phoneNumberReciever="5556";
        String message="Alarm Triggered";
        SmsManager sms = SmsManager.getDefault(); 
        sms.sendTextMessage(phoneNumberReciever, null, message, null, null);
        Toast.makeText(context," A message has been sent", Toast.LENGTH_LONG).show();

        Log.d("Alarm ", "Alarm Has been triggered and sms send");
    }
}

I have Already added required permissions in manifest.

Luke Willis
  • 8,429
  • 4
  • 46
  • 79
Rida Shahid
  • 386
  • 7
  • 22

2 Answers2

23

You are using an alarm type of ELAPSED_REALTIME_WAKEUP. That means that the second parameter to set() must be the number of milliseconds from now, where now is expressed as SystemClock.elapsedRealtime().

If your goal is to have this occur 10000 milliseconds from the time you make the set() call, that set() call should be:

alarmManager.set(alarmType, SystemClock.elapsedRealtime()+timeOrLengthofWait, alarmIntent);
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 4
    As an adition, the documentarion says: "If the trigger time you specify is in the past, the alarm triggers immediately." The timestamp of 10000 you provided in the question for sure represents a date in the past. This is the reason your alarm was triggering immediately. More info in https://developer.android.com/training/scheduling/alarms.html#set – androidevil Jul 13 '16 at 07:06
  • The comment above by @androidevil should really be the accepted answer. – Tigger Jan 09 '21 at 00:24
14

If you are creating PendingIntent of an alarm for past time it will be fired immediately. Example - Schedule alarm for today 8AM but executing code around 11AM will fire immediately.

Solution:

cal.add(Calendar.DATE, 1);

long delay = 24 * 60 * 60 * 1000;
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), delay,pendingIntent);` 

This will fire the event on next day at specified time (i.e 8AM);

Shripad Bhat
  • 2,373
  • 2
  • 19
  • 21
  • 6
    "If you are creating PendingIntent of an alarm for past time it will be fired immediately". Helped me a lot. – androidStud Mar 29 '17 at 18:40
  • Thanks. checked the documentation - A trigger time. If the trigger time you specify is in the past, the alarm triggers immediately.(ref https://developer.android.com/training/scheduling/alarms under Set a repeating alarm heading) – user1154390 Nov 05 '18 at 12:37