How to use Android AlarmManager with small intervals like 1 minute?

不打扰是莪最后的温柔 提交于 2019-11-29 07:56:50

I tried to set up AlarmManager with 1-2 minutes interval, but it looks like it fires randomly every several minutes.

Since you decided not to show how you "set up AlarmManager with 1-2 minutes interval", it will be difficult for anyone to help you. Please read the documentation for AlarmManager and note the default-inexact behavior new to Android 4.4.

I want to be safe from killing my background task by android, which would stop monitoring if I just use Service.

AlarmManager does not solve all problems in this regard. For example, if the user elects to force-stop your app (e.g., via Settings), your alarms are removed.

Is it possible to use AlarmManager in small, accurate intervals?

Use setRepeating() on Android 1.0-4.3 and setExact() on Android 4.4+. With setExact(), as part of processing one alarm event, you will need to schedule the next alarm event.

Would it be better to make Service with startForeground and partial WakeLock?

Only if your device is always plugged into power (e.g., industrial process monitor).

Posting a code snippet that we have used.Please modify as per your requirement. We use it for every 15 seconds and it works.

public class AlarmManagerBroadcastReceiver extends BroadcastReceiver  
{
private static final int _REFRESH_INTERVAL = 60 * 1; // 1 minutes
// Alarm id
    private static final int ALARM_ID = 102; // This can be any random integer.

    PendingIntent pi = null;
    AlarmManager am= null;

@Override
    public void onReceive(Context context, Intent intent) 
    {
        /* All actions to be handled here.. */
        }


    // This is to initialize the alarmmanager and the pending intent. It is done in separate method because, the same alarmmanager and
    // pending intent instance should be used for setting and cancelling the alarm.

    public void SetContext(Context context)
    {
        this.context = context;
        am=(AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
        pi = PendingIntent.getBroadcast(context, ALARM_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }

    // Setting the alarm to call onRecieve every _REFRESH_INTERVAL seconds
    public void SetAlarm()
    {
        // am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * _REFRESH_INTERVAL , pi);
        try
        {
            am.cancel(pi);
        }catch (Exception ignored){}
        am.setRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime(), 1000 * _REFRESH_INTERVAL , pi);
    }

    // Cancel the alarm.
    public void CancelAlarm()
    {
        am.cancel(pi);
    }

}
Calendar cal = Calendar.getInstance();
            cal.add(Calendar.SECOND, 30);
            Intent intent = new Intent(MainActivity.this, YourClass.class);
            PendingIntent pintent = PendingIntent.getService(MainActivity.this,
                    0, intent, 0);
            AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                    60* 1000, pintent);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!