Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Alarm manager working, but delayed

I would like to make a delay(10 min) for user then after it, user can edit something.

to do this,I created a setAlarm function :

public void setAlarm(Context context,int user,int time) {
   AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
   Intent intent = new Intent(context, sef_time.class);
   intent.putExtra(ONE_TIME, Boolean.FALSE);
   PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
   am.set(AlarmManager.RTC, 1000*60*time , pi); 
}

everything works fine, but my alarm manager has a delay. for example:

setAlarm(.....,int 10);

It has a delay : 00:10:03 second or 00:10:10 second 00:10:20 second !

where is my wrong ?

like image 745
S.M_Emamian Avatar asked Dec 28 '14 12:12

S.M_Emamian


People also ask

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.

How do I use alarm Manager on android?

This example demonstrates how do I use AlarmManager in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

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.


Video Answer


2 Answers

The easiest way to make system have a delay and then sound an alarm at the exact specified time is using setExact(), and the code can be something like this.

am.setExact(AlarmManager.RTC_WAKEUP,System.currentTimeMillis() + (time_you_want_to_delay_in_milliseconds) ,pi);
like image 198
Mohammed Abdul Bari Avatar answered Sep 18 '22 17:09

Mohammed Abdul Bari


As you can see here:

Beginning in API 19, the trigger time passed to this method is treated as inexact: the alarm will not be delivered before this time, but may be deferred and delivered some time later. The OS will use this policy in order to "batch" alarms together across the entire system, minimizing the number of times the device needs to "wake up" and minimizing battery use. In general, alarms scheduled in the near future will not be deferred as long as alarms scheduled far in the future.

With the new batching policy, delivery ordering guarantees are not as strong as they were previously. If the application sets multiple alarms, it is possible that these alarms' actual delivery ordering may not match the order of their requested delivery times. If your application has strong ordering requirements there are other APIs that you can use to get the necessary behavior; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent).

Applications whose targetSdkVersion is before API 19 will continue to get the previous alarm behavior: all of their scheduled alarms will be treated as exact.

If it's very important that the alarm be exact, use setExact (When the device's SDK is 19 or above).

like image 27
Assaf Gamliel Avatar answered Sep 19 '22 17:09

Assaf Gamliel