Alarm Manager - Scheduling multiple Non-repeating events

后端 未结 2 1275
借酒劲吻你
借酒劲吻你 2020-12-08 03:25

In Android Alarm Manager, how can we schedule multiple alarms which are non-repeating and do not have fixed intervals to repeat? I cannot use \'setRepeating\' function as th

2条回答
  •  遥遥无期
    2020-12-08 03:53

    As suggested @Jonathon Horsman, make sure that the intents you're creating are unique.

    If you want to set 10 alarms for example :

    for(int i=; i<10; i++) {
       Intent intent = new Intent(YourActivity.this,
                    YourAlarm.class);
       intent.setData(Uri.parse("timer:" + i);
       PendingIntent sender = PendingIntent.getBroadcast(
                    YourActivity.this, 0, intent,
                    Intent.FLAG_GRANT_READ_URI_PERMISSION);
       AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
       am.set(AlarmManager.RTC_WAKEUP, yourTimeInMillis, sender);
    }
    

    Worked fine for me.

提交回复
热议问题