问题描述
我正在开发一个小闹钟应用程序,我需要一种方法来计算时间差,以便能够区间传递给alarmManager。我看到了很多类似的话题在这里,所以问题是:什么是计算我的目的时差的最佳方法?
我写的方法,以便从timepicker获取时间和计算出的差异,但它是越野车,因为我得到一些疯狂的值,同时测试。
公众诠释CalculateInterval(){ / * ---从时间选择器获取目标的时间--- * /
VG的ViewGroup =(ViewGroup中)tp.getChildAt(0);
ViewGroup中数字1 =(ViewGroup中)vg.getChildAt(0);
ViewGroup中号码2 =(ViewGroup中)vg.getChildAt(1);
串个小时=((EditText上)number1.getChildAt(1))。gettext的()
的ToString();
串分钟=((的EditText)number2.getChildAt(1))。的getText()
的ToString(); / * ---转换为整数值以毫秒--- * /
INT hrsInMillis =的Integer.parseInt(小时)* 3600 * 1000;
INT mnsInMillis =的Integer.parseInt(分钟)* 60 * 100; / * ---获得当前的时间(ms)--- * /
日历C = Calendar.getInstance();
INT secondsInMillis = c.get(Calendar.SECOND)* 1000;
INT minutesInMillis = c.get(Calendar.MINUTE)* 1000 * 60;
INT HoursInMillis = c.get(Calendar.HOUR)* 1000 * 3600; INT电流= secondsInMillis + minutesInMillis + HoursInMillis; / * ---计算差值--- * /
INT间隔=(hrsInMillis + mnsInMillis) - 电流; 返回区间;
}
日历C = Calendar.getInstance();
INT secondsInMillis = c.get(Calendar.SECOND)* 1000;
INT minutesInMillis = c.get(Calendar.MINUTE)* 1000 * 60;
INT HoursInMillis = c.get(Calendar.HOUR)* 1000 * 3600;
以上code是有点多余,我猜这就是为什么你越来越疯狂的价值观。相反,获得的
和刚与另一长
C值长$减去它C $ C>。
不过,这是一个有点技巧,因为你目前只获得米利斯小时和分钟。
下面就是我想要做的(伪code):
设置报警Calendar.getInstance()
设置alarm.hour到的Integer.parseInt(小时)
alarm.minute设置的Integer.parseInt(分钟)
集alarm.secs为0
如果alarm.before(现)
加1天报警
万一
设置差异(alarm.getTimeInMillis() - now.getTimeInMillis())
I'm developing a small alarm clock app and I need a way to calculate the time difference to be able to pass the interval to the alarmManager. I saw a lot of similar topics around here, so the question is: what is the best way of calculating the time difference for my purpose?I wrote that method to obtain time from the timepicker and calculate the difference but it's buggy since I get some crazy values while testing..
public int CalculateInterval() {
/*--- get the target time from the time picker ---*/
ViewGroup vg = (ViewGroup) tp.getChildAt(0);
ViewGroup number1 = (ViewGroup) vg.getChildAt(0);
ViewGroup number2 = (ViewGroup) vg.getChildAt(1);
String hours = ((EditText) number1.getChildAt(1)).getText()
.toString();
String mins = ((EditText) number2.getChildAt(1)).getText()
.toString();
/*--- convert to integer values in ms ---*/
int hrsInMillis = Integer.parseInt(hours) * 3600 * 1000;
int mnsInMillis = Integer.parseInt(mins) * 60 * 100;
/*--- obtain the current time in ms ---*/
Calendar c = Calendar.getInstance();
int secondsInMillis = c.get(Calendar.SECOND) * 1000;
int minutesInMillis = c.get(Calendar.MINUTE) * 1000 * 60;
int HoursInMillis = c.get(Calendar.HOUR) * 1000 * 3600;
int current = secondsInMillis + minutesInMillis + HoursInMillis;
/*--- calculate the difference ---*/
int interval = (hrsInMillis + mnsInMillis) - current;
return interval;
}
Calendar c = Calendar.getInstance();
int secondsInMillis = c.get(Calendar.SECOND) * 1000;
int minutesInMillis = c.get(Calendar.MINUTE) * 1000 * 60;
int HoursInMillis = c.get(Calendar.HOUR) * 1000 * 3600;
The above code is a bit unnecessary, and I would guess this is why you are getting "crazy values". Instead, get the long
value from c
and just subtract it with another long
.
However, this is a bit of a trick since you currently only get the hours and minutes in millis.
Here's what I'd do (pseudocode):
set alarm to Calendar.getInstance()
set alarm.hour to Integer.parseInt(hours)
set alarm.minute to Integer.parseInt(mins)
set alarm.secs to 0
if alarm.before(now)
add 1 day to alarm
endif
set difference to (alarm.getTimeInMillis() - now.getTimeInMillis())
这篇关于设置报警android的计算时间差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!