Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android中的多个警报_Android_Alarmmanager - Fatal编程技术网

android中的多个警报

android中的多个警报,android,alarmmanager,Android,Alarmmanager,我正在用android做一个项目,我想创建多天的警报,但问题是当我选择多天时,警报只会在最近一天显示。 这是我的尝试 public void onClick(View v) { CheckBox box1=(CheckBox)findViewById(R.id.checkBox1); CheckBox box2=(CheckBox)findViewById(R.id.checkBox2); CheckBox b

我正在用android做一个项目,我想创建多天的警报,但问题是当我选择多天时,警报只会在最近一天显示。 这是我的尝试

public void onClick(View v) {
            CheckBox box1=(CheckBox)findViewById(R.id.checkBox1);
            CheckBox box2=(CheckBox)findViewById(R.id.checkBox2);       
            CheckBox box3=(CheckBox)findViewById(R.id.checkBox3);       
            CheckBox box4=(CheckBox)findViewById(R.id.checkBox4);       
            CheckBox box5=(CheckBox)findViewById(R.id.checkBox5);       
            CheckBox box6=(CheckBox)findViewById(R.id.checkBox6);       
            CheckBox box7=(CheckBox)findViewById(R.id.checkBox7);       

            /** Getting a reference to TimePicker object available in the MainActivity */
            TimePicker tpTime = (TimePicker) findViewById(R.id.tp_time);
            int hour = tpTime.getCurrentHour();
            int minute = tpTime.getCurrentMinute();

            Calendar c=Calendar.getInstance();
            int month;
            int day;
            int year;

            Date date =new Date();
            c.setTime(date);

            if(box1.isChecked()) {
                /** This intent invokes the activity DemoActivity, which in turn opens the AlertDialog window */
                Intent i = new Intent("com.example.servicealarmdemo2.demoactivity");
                /** Creating a Pending Intent */
                PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);
                c.setTime(date);
                c.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);


                month=c.get(Calendar.MONTH);
                day=c.get(Calendar.DATE);
                year=c.get(Calendar.YEAR);

                /** Getting a reference to the System Service ALARM_SERVICE */
                AlarmManager alarmManager1 = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);
               /** Creating a calendar object corresponding to the date and time set by the user */
                GregorianCalendar calendar = new GregorianCalendar(year,month,day, hour, minute);

                /** Converting the date and time in to milliseconds elapsed since epoch */
                long alarm_time = calendar.getTimeInMillis();

                /** Setting an alarm, which invokes the operation at alart_time */
                alarmManager1.set(AlarmManager.RTC_WAKEUP  , alarm_time , operation);
                alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP,alarm_time,7*24*3600*1000, operation);
            }

            if(box2.isChecked()) {
                Intent i = new Intent("com.example.servicealarmdemo2.demoactivity");
                PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);
                c.setTime(date);
                c.set(Calendar.DAY_OF_WEEK,Calendar.TUESDAY);

                month=c.get(Calendar.MONTH);
                day=c.get(Calendar.DATE);
                year=c.get(Calendar.YEAR);                  
                AlarmManager alarmManager2 = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);
                GregorianCalendar calendar = new GregorianCalendar(year,month,day, hour, minute);
                long alarm_time = calendar.getTimeInMillis();
                alarmManager2.set(AlarmManager.RTC_WAKEUP  , alarm_time , operation);
                alarmManager2.setRepeating(AlarmManager.RTC_WAKEUP,alarm_time,7*24*3600*1000, operation);

            }
如果您的代码中有任何帮助,我们将不胜感激


您对所有人都使用相同的报警ID,因此之前的alrm会覆盖我们的不同报警ID以获得不同。

我可能遗漏了一些内容,但检查
box1
box2
之间的区别是什么?我想可能会回答您的问题。使用相同的
pendingent
将替换警报。复选框中还提到了我的应用程序检查日期并为所选日期创建警报的日期。我将删除选中框(x)和框(y)时处理的部件之间的共同点,我可以看到,您的大部分代码:)可能重复谢谢我已更改它现在部分工作,但在选择当天的前一天时仍有一个错误我直接收到警报。因此,如果tdy是星期二,我将在星期一设置警报,我将直接收到警报。对此问题有何想法?那么我应该比较选择的日期吗今天怎么样?是的。以前触发的警报自然会直接触发。因此,您必须验证是否为将来创建了任何新警报。是的,如果警报是在设置警报时触发的过去时间,则Lmickos是正确的,因此请确保您的警报设置为未来时间。我通过将当前日期(毫秒)与指定日历上设置的警报进行比较来解决问题,并在报警设置在比当前时间大的特定时间。感谢您的回复:)
PendingIntent pi = PendingIntent.getActivity(context, **AlarmID**,
                    intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

// for trigger alarm on day 
 Calendar calendar = Calendar.getInstance();

            calendar.set(Calendar.DAY_OF_WEEK,alarmday);
            calendar.set(Calendar.HOUR, hour);
            calendar.set(Calendar.MINUTE,minute );
            calendar.set(Calendar.AM_PM,am-pm);
            calendar.set(Calendar.SECOND, 0);


                am.setRepeating(AlarmManager.RTC_WAKEUP,
                    calendar.getTimeInMillis(),604800000, pi);