安卓alarmmanager多个报警,一是覆盖其他? [英] android alarmmanager multiple alarms, one overwrites the other?

查看:156
本文介绍了安卓alarmmanager多个报警,一是覆盖其他?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索职位,回答我的问题,但没有发现任何解决我的问题。我试图设置使用一个AlarmSettings类3种不同的报警。当我设置两个报警,第二个取precedence在第一和第一永不熄灭。我想这可能都与我的悬而未决的意图......我真的很新Android和将大大AP preciate的帮助。这是我的code设置报警:

 公共无效setAlarm(){

        台历挂历= 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);

        如果(timepicker.getCurrentHour()&其中; calendar.get(Calendar.HOUR_OF_DAY)){//如果报警小时小于目前小时
            calendar.add(Calendar.DATE,1); //再加入24小时(1 DATE或日)
        }

        //创建我们要TextView的alarmtime设置在主文本
        StringBuilder的SB =新的StringBuilder();
        如果(timepicker.getCurrentHour()> 12){
            sb.append(timepicker.getCurrentHour() -  12);
        } 其他 {
            sb.append(timepicker.getCurrentHour());
        }
        sb.append(:);
        sb.append(timepicker.getCurrentMinute());
        sb.append();
        如果(timepicker.getCurrentHour()> 12){
            sb.append(下午);
        } 其他 {
            sb.append(我);
        }

        如果(((GlobalVariables)getApplication())。getCurrentAlarmBeingEdited()== 1){
            ((GlobalVariables)getApplication())setAlarm1Cal(日历)。
            Main.alarmTime1.setText(某人);

            意图int​​ent1 =新的意图(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);

        }否则如果(((GlobalVariables)getApplication())。getCurrentAlarmBeingEdited()== 2){
            ((GlobalVariables)getApplication())setAlarm2Cal(日历)。
            Main.alarmTime2.setText(某人);

            意图int​​ent2 =新的意图(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);

        }否则如果(((GlobalVariables)getApplication())。getCurrentAlarmBeingEdited()== 3){
            ((GlobalVariables)getApplication())setAlarm3Cal(日历)。
            Main.alarmTime3.setText(某人);

            意图int​​ent3 =新的意图(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);
        }

        完();

        Toast.makeText(getApplicationContext(),系统时间:+ System.currentTimeMillis的()+\ N+采摘时间:+ calendar.getTimeInMillis(),Toast.LENGTH_LONG).show();
    }
 

解决方案

  PendingIntent pendingIntent1 = PendingIntent.getActivity(getApplicationContext(),0,intent1,PendingIntent.FLAG_ONE_SHOT);
 

改变0属性的ID为你的报警,例如,你有三个报警,

与0,1,2重复上面的code。

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

I've searched posts for answers to my question but haven't found anything that solves my problem. I'm trying to set 3 different alarms using one AlarmSettings class. When I set two alarms, the second one takes precedence over the first and the first never goes off. I think it may have to do with my pending intent... I'm really new to android and would greatly appreciate the help. Here's my code for setting the alarms:

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);

change the 0 attribute to an id for your alarm, for example you have three alarms,

repeat the above code with 0,1,2.

this way they won't override each other.

这篇关于安卓alarmmanager多个报警,一是覆盖其他?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆