简体   繁体   English

如何在 Android 中使用 AlarmManager 设置多个警报?

[英]How can I set more than one alarm with AlarmManager in Android?

This is my first app using AlarmManager, I´m triyng to set multiple alarms from an array of dates.这是我第一个使用 AlarmManager 的应用程序,我正在尝试从一组日期中设置多个警报。 For the array of dates I am occupying a String format对于日期数组,我使用字符串格式

   String[] date = new String[3];
    date[0] = "2022-01-11 13:10";
    date[1] = "2022-01-11 12:30";
    date[2] = "2022-01-12 12:30";  

And I convert it to Date(long) to set it to the alarmManager:我将其转换为 Date(long) 以将其设置为 alarmManager:

 @RequiresApi(api = Build.VERSION_CODES.O)
public Long parseDateLong(String myDate){
    long millisSinceEpoch = LocalDateTime.parse(myDate, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"))
            .atOffset(ZoneOffset.UTC)
            .toInstant()
            .toEpochMilli();
    return millisSinceEpoch;
}            

This is the service that does the onRecive,这是执行 onRecive 的服务,

 @Override
public void onReceive(Context context, Intent intent) {
    Intent service1 = new Intent(context, NotificationService.class);
    service1.setData((Uri.parse("custom://" + System.currentTimeMillis())));
    ContextCompat.startForegroundService(context, service1 );
    Log.i("ALARM", " ALARM RECEIVED!!!");

}

I am using code I found to iterate over the array, and set the alarm:我正在使用我找到的代码来迭代数组,并设置警报:

AlarmManager[] alarmManager=new AlarmManager[date.length];
               ArrayList intentArray = new ArrayList<PendingIntent>();
                for(int i=0; i<date.length; i++){
                    Intent intent = new Intent(AddMedication.this, AlarmReceiver.class);
                    PendingIntent pi=PendingIntent.getBroadcast(AddMedication.this, i ,intent, 0);

                    Log.i("dates", "dates"+date[i]);

                    alarmManager[i] = (AlarmManager) getSystemService(ALARM_SERVICE);
                    alarmManager[i].set(AlarmManager.RTC_WAKEUP, parseDateLong(date[i]),pi);

                    intentArray.add(pi);

                }

The problem is when I set the date array, all the alarms are executed instantly, without respecting the dates and times indicated.问题是当我设置日期数组时,所有警报都会立即执行,而不考虑指示的日期和时间。

How can I set multiple alarms and they run at the times indicated in the array?如何设置多个警报并在阵列中指示的时间运行?

you are initialising AlarmManager in a very wrong way.您正在以非常错误的方式初始化AlarmManager this component can't be created by developer using new keyword (I'm surprised that this is even possible with some public constructor), only obtained from system/ Context这个组件不能由开发人员使用new关键字创建(我很惊讶这甚至可以使用一些public构造函数),只能从 system/ Context获得

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

and there is only one instance of it, all alarms should be sheduled to this single one instead of items in array并且只有一个实例,所有警报都应该发送到这个单个而不是数组中的项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM