1

I am trying to put an AlarmManager to run every 3 min but it is running at wrong and varied times, moments in seconds and others does not run. I am trying to test on an Android 7.0 and another 6.0 device and both are running wrong, I saw the following comments but could not fix.

Alarm Manager Example AlarmManager fires alarms at wrong time Android AlarmManager fire at wrong time

The following code:

long repeatTime = 180000;

        AlarmManager processTimer = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intentAlarm = new Intent(context, TimerProcessReceiver.class);
        PendingIntent pendingIntentAlarm = PendingIntent.getBroadcast(context, 0,
                intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);

        if (android.os.Build.VERSION.SDK_INT < 19) {
            processTimer.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
                    repeatTime, pendingIntentAlarm);
        } else {
            processTimer.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() +
                    repeatTime, pendingIntentAlarm);
        }

Still having problems, I've updated as above. Update as @Vyacheslav's reply

long repeatTime = 180000 + System.currentTimeMillis();
    AlarmManager processTimer = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intentAlarm = new Intent(context, ProcessReceiver.class);
    PendingIntent pendingIntentAlarm = PendingIntent.getBroadcast(context, 0,
            intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);

    int currentapiVersion = Build.VERSION.SDK_INT;

    if (Build.VERSION.SDK_INT >= 23) {
        processTimer.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
                repeatTime, pendingIntentAlarm);

    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

            processTimer.setExact(AlarmManager.RTC_WAKEUP,
                    repeatTime, pendingIntentAlarm);

    } else if (currentapiVersion < Build.VERSION_CODES.KITKAT) {

        processTimer.set(AlarmManager.RTC_WAKEUP,  repeatTime,
                pendingIntentAlarm);

    }

In case I am using two simultaneous timers with the PendingIntent of ids 0 and 1 (but the structure of adding these PendingIntent are the same as the code above) but with the same runtime 3 min. Both are executing the wrong way in a few seconds and randomly.

user6505882
  • 247
  • 3
  • 16

1 Answers1

0

Use setExactAndAllowWhileIdle(int type, long triggerAtMillis, PendingIntent operation) instead. It helps to wakeup your device.

EDIT

 int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion < android.os.Build.VERSION_CODES.KITKAT){
            am.set(AlarmManager.RTC_WAKEUP, nexttime, pi);
        } else {
            if (currentapiVersion < android.os.Build.VERSION_CODES.M) {
                am.setExact(AlarmManager.RTC_WAKEUP, nexttime, pi);
            } else {
                    am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, nexttime, pi);
            }
        }
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
  • The setRepeating and setExact, which ones would work for devices below the API level 23?In case I am testing these devices at level 23 but I will test at lower levels as well. Sorry for the mistake. – user6505882 May 31 '17 at 12:47
  • @user6505882 I'm using this: look at updated answer. – Vyacheslav May 31 '17 at 12:51
  • @user6505882 due to OS restrictions this is not possible to improve the presition. ~1-2second is normal in the most cases. In new OS versions battery saver mode exists – Vyacheslav May 31 '17 at 14:33
  • @user6505882 this is the maximum you can achieve – Vyacheslav May 31 '17 at 14:33
  • So far no solution, has anyone managed to make this AlarmManager work with two PendingIntents with timers running independently at the correct time? One of the timers will die at one point in the application but the other will continue, but neither is working within the 3 minute time. – user6505882 May 31 '17 at 17:04