strings95
strings95

Reputation: 681

alarmmanager triggers a pending intent not on time

i'm developing an android project which simply want to have an alarm every day at specific time. for this aim, i'm using alarmmanger in a service. i add my pending intent to alarmmanger and set my time like below:

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, h);
    calendar.set(Calendar.MINUTE, m);

    Intent i = new Intent(DefaultActivity.fullname(Actions.TIME_CHANGED));

    i.putExtra(TIME_NAME, name);
    i.putExtra(TIME_CLOCK, clock);


    PendingIntent pendingIntent = PendingIntent.getService(ctx, index, i, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); 

the problem is when ever i call my service i got "Actions.TIME_CHANGED"

my startCommand is like this:

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
String intentAction = intent == null ? null : intent.getAction();

if (intentAction != null)
{
    Log.e(MainActivity.WATERMELON_TAG, "action: " + intentAction);

    if (intentAction.equals(DefaultActivity.fullname(Actions.TIME_CHANGED)))
    {
        clockIsChanged(intent);
    }

    if (intentAction.equals(DefaultActivity.fullname(Actions.TIME_RENEW_CHECKING)))
    {
        reNew();
    }

    if (intentAction.equals(DefaultActivity.fullname(Actions.FIRST_TIME_BOOT)))
    {
        reNew();
    }
}

return START_STICKY;
}

i don't know what is the problem. my solution is i add a function to check is the pendingintent time is equals to current time execute my tasks but it's not a good solution.

why this happen?

thanks in advance

Upvotes: 0

Views: 190

Answers (1)

Gumbo
Gumbo

Reputation: 1746

The documentation says:

Note: as of API 19, all repeating alarms are inexact.

So if you are using a API higher than 19, you should better use AlarmManager.setExact and resceduel the alarm in your onReceive-Method.

Another posibility is using an API-Level lower than 19.

Upvotes: 2

Related Questions