当前位置: 首页 > 面试题库 >

android alarmmanager多个警报,一个覆盖另一个?

拓拔弘化
2023-03-14
问题内容

我已在帖子中搜索了我的问题的答案,但没有找到能解决我问题的任何东西。我正在尝试使用一个AlarmSettings类设置3种不同的警报。当我设置两个警报时,第二个警报优先于第一个警报,第一个永不熄灭。我认为这可能与我的未决意图有关……我真的是android新手,非常感谢您的帮助。这是我设置警报的代码:

public void setAlarm() {

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, timepicker.getCurrentHour());
        calendar.set(Calendar.MINUTE, timepicker.getCurrentMinute());
        calendar.set(Calendar.SECOND, 0);

        if (timepicker.getCurrentHour() < calendar.get(Calendar.HOUR_OF_DAY)) { //if the alarm hour is less than the current hour
            calendar.add(Calendar.DATE, 1);                                     //then add 24 hours (1 DATE or Day)                 
        }

        //Create the text that we want to set the TextView alarmtime to in Main
        StringBuilder sb = new StringBuilder();
        if (timepicker.getCurrentHour() > 12) {
            sb.append(timepicker.getCurrentHour() - 12);
        } else {
            sb.append(timepicker.getCurrentHour());
        }
        sb.append(":");
        sb.append(timepicker.getCurrentMinute());
        sb.append(" ");
        if (timepicker.getCurrentHour() > 12) {
            sb.append("pm");
        } else {
            sb.append("am");
        }

        if (((GlobalVariables)getApplication()).getCurrentAlarmBeingEdited() == 1) {
            ((GlobalVariables)getApplication()).setAlarm1Cal(calendar);
            Main.alarmTime1.setText(sb);

            Intent intent1 = new Intent(AlarmSettings.this, AlarmReceiver.class);
            intent1.putExtra("alarm_num", ((GlobalVariables)getApplication()).getCurrentAlarmBeingEdited());
            PendingIntent pendingIntent1 = PendingIntent.getActivity(getApplicationContext(), 0, intent1, PendingIntent.FLAG_ONE_SHOT);

            alarmmanager1.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent1);

        } else if (((GlobalVariables)getApplication()).getCurrentAlarmBeingEdited() == 2) {
            ((GlobalVariables)getApplication()).setAlarm2Cal(calendar);
            Main.alarmTime2.setText(sb);

            Intent intent2 = new Intent(AlarmSettings.this, AlarmReceiver.class);
            intent2.putExtra("alarm_num", ((GlobalVariables)getApplication()).getCurrentAlarmBeingEdited());
            PendingIntent pendingIntent2 = PendingIntent.getActivity(getApplicationContext(), 0, intent2, PendingIntent.FLAG_ONE_SHOT);

            alarmmanager2.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent2);

        } else if (((GlobalVariables)getApplication()).getCurrentAlarmBeingEdited() == 3) {
            ((GlobalVariables)getApplication()).setAlarm3Cal(calendar);
            Main.alarmTime3.setText(sb);

            Intent intent3 = new Intent(AlarmSettings.this, AlarmReceiver.class);
            intent3.putExtra("alarm_num", ((GlobalVariables)getApplication()).getCurrentAlarmBeingEdited());
            PendingIntent pendingIntent3 = PendingIntent.getActivity(getApplicationContext(), 0, intent3, PendingIntent.FLAG_ONE_SHOT);

            alarmmanager3.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent3);
        }

        finish();

        Toast.makeText(getApplicationContext(), "system time: " + System.currentTimeMillis() + "\n" + "picked time: " + calendar.getTimeInMillis(), Toast.LENGTH_LONG).show();      
    }

问题答案:
PendingIntent pendingIntent1 = PendingIntent.getActivity(getApplicationContext(), 0, intent1, PendingIntent.FLAG_ONE_SHOT);

将0属性更改为警报的ID,例如,您有三个警报,

用0,1,2重复上述代码。

这样,他们就不会互相覆盖。



 类似资料:
  • 问题内容: 在将一个人叠加到另一个人上时,我需要帮助。 我的代码如下所示: 不幸的是,我不能将或嵌套在第一个内部。 如图所示,它必须是两个单独的,但是我需要知道如何将放置在上方和最右侧,并以的顶部为中心。 问题答案: 我建议使用来了解和子元素。

  • 在网上搜索了几个小时后,我找到了一些关于如何在另一个模式中打开一个新模式的解决方案。但我的要求有点不同。我想使用一个有点“通用”的模态表单,它将在我的页面(不是整个应用程序,仅用于当前页面)中充当消息框。一旦调用/显示,此消息框将全部覆盖。 例如,我为我的数据输入打开一个模态表单,如果我想提示一条消息给用户,我会在数据输入表单上弹出另一个模态表单。 我是web编程的新手,所以我不知道我的代码哪里出

  • 程序屏幕截图 我正在制作动画。当我按下按钮时,它会触发一个动画,使从右侧浮动。当鼠标离开时JPanel退出动画),但问题是我在动画上有一个。因此,当我将鼠标移出动画时,面板不仅会消失,而且当我在按钮(这是面板的一个组件)上移动鼠标时,面板也会消失,这意味着当我想单击按钮时,它会消失,因为当鼠标离开时,就会被触发。 请看上面的图片,我在其中标记了某些区域。 < li >是JPanel,当我将鼠标移到

  • 问题内容: 所以,这就是我正在做的: 我有一张桌子,上面放着所有的桌子。 但是在一个特殊的场合,我希望一个特定的对象在各个方向都有,我将其设置为。 显然,我将CSS样式依次放在一个外部文件中。 但是渲染的CSS仅具有,并且似乎已被覆盖! 请说明: 这是怎么发生的以及为什么发生? 我应该如何安排这些规则来解决我的问题(除了内联样式)? 编辑:我在表中删除之前。我从来没有使用过它,我只是在这里提到它以

  • 在这段代码中,我试图将一个导航栏(content1)覆盖在另一个导航栏(content)之上,但似乎什么都不起作用。我做错了什么?即使我可以使其覆盖,它们也不是以容器为中心放置的。 null null 只是想知道如何将我的导航条覆盖在另一个导航条之上并使其在容器中居中,

  • 问题内容: 我的项目是对齐航空照片,以制作出一张马赛克地图。我的计划是从两张照片开始,将第二张照片与第一张照片对齐,然后从两张对齐的图像中创建一个“初始镶嵌”。完成此操作后,我将第三张照片与初始镶嵌图对齐,然后将第四张照片与该图的结果对齐,以此类推,从而逐步构建地图。 我有两种技术可以做到这一点,但是使用的更精确的技术仅适用于两个图像阶段,因为两个输入图像的大小必须相同。因此,我尝试了一种新的解决