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

前端 未结 3 454
执念已碎
执念已碎 2020-12-18 07:09

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,

3条回答
  •  北海茫月
    2020-12-18 07:38

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

提交回复
热议问题