我从时间选择器中抽出时间,然后将时间与当前时间进行比较。
但不知道为什么它不起作用。
以下是从timepicker
获取时间的代码。
Add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
hour=timePicker.getCurrentHour();
if(hour>=12){
reqformat="PM";
}
else
{
reqformat="AM";
}
if(hour==0)
{
hour=12;
}
hour%=12;
minute=timePicker.getCurrentMinute();
timePicker.setEnabled(false);
handler.post(r);
Toast.makeText( getApplicationContext() , String.valueOf(hour) ,Toast.LENGTH_LONG ).show();
}
catch (Exception e){
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
}
}}
此处使用的Runnable
代码,
private Runnable r=new Runnable() {
@Override
public void run() {
currhour=calendar.get(Calendar.HOUR_OF_DAY);
if(currhour>=12){
currformat="PM";
}
else
{
currformat="AM";
}
currhour%=12;
if(currhour==0&&currformat.equals("PM")){
currhour=12;
}
if(hour==currhour&&(minute==calendar.get(Calendar.MINUTE))&&(currformat.equals(reqformat))){
handler.removeCallbacks(this);
timePicker.setEnabled(true);
Toast.makeText(getApplicationContext(),"Alarm Ringing",Toast.LENGTH_LONG).show();
}else {
handler.postDelayed(this, 1000);
}
}
};