How to use Android AlarmManager with small interva

2019-01-25 21:19发布

I want to make some external service monitor and be notified on problems as fast as possible.

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

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

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

Which approaches are used in applications like Facebook, Gmail to notify about new messages?

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

3条回答
迷人小祖宗
2楼-- · 2019-01-25 21:58

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);
    }

}
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-25 21:58
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);
查看更多
唯我独甜
4楼-- · 2019-01-25 22:07

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).

查看更多
登录 后发表回答