首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >android警报管理器多个警报,其中一个覆盖另一个?

android警报管理器多个警报,其中一个覆盖另一个?
EN

Stack Overflow用户
提问于 2012-04-23 03:38:54
回答 1查看 10.9K关注 0票数 17

我已经搜索了我的问题的答案的帖子,但没有找到任何解决我的问题的东西。我正在尝试使用一个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();      
    }
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10271417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档