我已将闹钟的开始时间和结束时间设置如下:
Calendar startalarmTime = Calendar.getInstance();
startalarmTime.setTimeInMillis(System.currentTimeMillis());
startalarmTime.set(Calendar.HOUR_OF_DAY, 11);
startalarmTime.set(Calendar.MINUTE, 30);
startalarmTime.set(Calendar.SECOND, 0);
Calendar endalarmTime = Calendar.getInstance();<br>
endalarmTime.setTimeInMillis(System.currentTimeMillis());
endalarmTime.set(Calendar.HOUR_OF_DAY, 11);
endalarmTime.set(Calendar.MINUTE, 31);
endalarmTime.set(Calendar.SECOND, 0);
int repeatingInterval = 5; //5 seconds
long endTime = endalarmTime.getTimeInMillis();
long startTime = startalarmTime.getTimeInMillis();
我正在设置重复闹钟如下:
Intent broadCast = new Intent(ctx,AlarmBroadCaster.class);
broadCast.putExtra("endTime", endTime);
pIntent = PendingIntent.getBroadcast(ctx, 12345, broadCast, Intent.FILL_IN_DATA);
AlarmManager alrmMgr = (AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);
alrmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,startTime,repeatInterval*1000, pIntent);
警报在11:30响起,并且每5秒重复一次,这很好。
要在11:31取消闹钟,我在BroadCastReceiver的onReceive()中写了以下代码,如下所示:
public void onReceive(){
if(System.currentTimeMillis()==alarmEndTime){
AlarmManager alarmManager = (AlarmManager)context.getSystemService(context.ALARM_SERVICE);
alarmManager.cancel(pIntent);
Toast.makeText(context,"Cancelled Endtime",Toast.LENGTH_LONG).show();
}
}
AndroidManifest.xml:
<receiver android:name="com.andr.broadcastreceiverdemo.AlarmBroadCaster"></receiver>
警报在到达结束时间时永远不会取消。
任何人都可以帮助我在一段时间内取消闹钟。
答案 0 :(得分:0)
将其更改为 if(System.currentTimeMillis()&gt; alarmEndTime)在系统时间超过警报结束时间后立即停止。 ==检查大部分时间是否完全等于和错过。