Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alarm manager triggered immediately

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.

like image 294
Rida Shahid Avatar asked Jul 19 '13 21:07

Rida Shahid


People also ask

What is alarm manager in Android?

android.app.AlarmManager. This class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future.

How do I turn off alarm manager on Android?

In order to delete : AlarmManager alarmManager = (AlarmManager) getSystemService(Context. ALARM_SERVICE); Intent myIntent = new Intent(getApplicationContext(), SessionReceiver. class); PendingIntent pendingIntent = PendingIntent.

Does alarm Manager persist even after reboot?

The AlarmManager service is a convenient class to schedule working on your Android application; however, when the device shuts down or reboots, all your alarms will be lost since the system does not retain them between system restarts.

What is exact alarm?

The new exact alarm permission ( SCHEDULE_EXACT_ALARM ) was created to save system resources. Alarms that must be executed in an exact time may be triggered when the phone is on power-saving mode or Doze, making the app consumes more battery than it should.


1 Answers

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);
like image 57
CommonsWare Avatar answered Oct 30 '22 21:10

CommonsWare