Android的报警器立即衬托 [英] Android alarm sets off immediately

查看:143
本文介绍了Android的报警器立即衬托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么报警掀起立即而不是在指定的时间?

 意图myIntent =新意图(Notepad.this,MyAlarmService.class);
的PendingIntent的PendingIntent = PendingIntent.getService(Notepad.this,0,myIntent,0);AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE);
         台历挂历= Calendar.getInstance();
         calendar.setTimeInMillis(System.currentTimeMillis的());
         calendar.set(Calendar.YEAR,2012);
         calendar.set(的Calendar.MONTH,4);
         calendar.set(Calendar.DAY_OF_MONTH,5);
         calendar.set(Calendar.HOUR_OF_DAY,10);
         calendar.set(Calendar.MINUTE,30);
         calendar.set(Calendar.SECOND,4);
     alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),的PendingIntent);

如果我只用 calendar.add(Calendar.SECOND,5); ,这5秒后衬托

根据尼古拉斯的回答SOLUTION

 日期DAT =新的日期(); //初始化到现在
        日历cal_alarm = Calendar.getInstance();
        日历cal_now = Calendar.getInstance();
        cal_now.setTime(DAT);
        cal_alarm.setTime(DAT);
        cal_alarm.set(Calendar.HOUR_OF_DAY,Integer.valueOf(H));
        cal_alarm.set(Calendar.MINUTE,Integer.valueOf(米));
        cal_alarm.set(Calendar.SECOND,0);        如果(cal_alarm.before(cal_now)){
            cal_alarm.add(Calendar.DATE,1);
        }


解决方案

要我看来,要设置报警时间日历时间和日期。或许设置了两个不同的日历,并有一个用于当前时间和另一闹铃时间。这是否有意义?

Why does the alarm set off immediately instead of the specified time?

Intent myIntent = new Intent(Notepad.this, MyAlarmService.class);
PendingIntent pendingIntent = PendingIntent.getService(Notepad.this, 0, myIntent, 0);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
         Calendar calendar = Calendar.getInstance();
         calendar.setTimeInMillis(System.currentTimeMillis());
         calendar.set(Calendar.YEAR, 2012);
         calendar.set(Calendar.MONTH, 4);
         calendar.set(Calendar.DAY_OF_MONTH, 5);
         calendar.set(Calendar.HOUR_OF_DAY, 10);
         calendar.set(Calendar.MINUTE, 30);
         calendar.set(Calendar.SECOND, 4);
     alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

If I use only calendar.add(Calendar.SECOND, 5);, it sets off after 5 seconds.

SOLUTION based on Nicholas' answer

        Date dat  = new Date();//initializes to now
        Calendar cal_alarm = Calendar.getInstance();
        Calendar cal_now = Calendar.getInstance();
        cal_now.setTime(dat);
        cal_alarm.setTime(dat);
        cal_alarm.set(Calendar.HOUR_OF_DAY,Integer.valueOf(h));
        cal_alarm.set(Calendar.MINUTE, Integer.valueOf(m));
        cal_alarm.set(Calendar.SECOND,0);

        if(cal_alarm.before(cal_now)){
            cal_alarm.add(Calendar.DATE,1);
        }

解决方案

To me it seems that you are setting the calendar time and day to the alarm time. Maybe set up two different calendars and have one for the current time and another for the alarm time. Does that make sense?

这篇关于Android的报警器立即衬托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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