Android 如何设置特定时段的报警
我允许用户设置重复报警的开始和结束时间,例如每天下午2:30到4:30。我可以将警报设置为在指定的开始时间关闭,但如何在结束时间停止警报 我想我应该在Broadcast Receiver类中处理这个问题,在那里我得到额外的时间并将其与系统时间进行比较,但我不能像在活动中取消警报那样取消警报 这是我如何设置警报的一个示例:Android 如何设置特定时段的报警,android,alarmmanager,Android,Alarmmanager,我允许用户设置重复报警的开始和结束时间,例如每天下午2:30到4:30。我可以将警报设置为在指定的开始时间关闭,但如何在结束时间停止警报 我想我应该在Broadcast Receiver类中处理这个问题,在那里我得到额外的时间并将其与系统时间进行比较,但我不能像在活动中取消警报那样取消警报 这是我如何设置警报的一个示例: Intent intent = new Intent(NewSchedule.this, RepeatingAlarm.class); intent.putEx
Intent intent = new Intent(NewSchedule.this, RepeatingAlarm.class);
intent.putExtra("endTime", cal_alarmend.getTimeInMillis()/1000);
PendingIntent sender = PendingIntent.getBroadcast(NewSchedule.this, REQUEST_CODE, intent, 0);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal_alarmstart.getTimeInMillis(), 6000, sender);
接收者类别:
public class RepeatingAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "repeating_received", Toast.LENGTH_SHORT).show();
long now = System.currentTimeMillis();
Bundle b = intent.getExtras();
long end = b.getLong("endTime");
Log.i("now", now + "");
Log.i("end", end + "");
if (now > end)
{
???????
}
}
}
编辑
为什么我不能取消BroadcastReceiver中onReceive()中的报警
public class RepeatingAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "repeating_received", Toast.LENGTH_SHORT).show();
long now = System.currentTimeMillis();
Bundle b = intent.getExtras();
long end = b.getLong("endTime");
int rc = b.getInt("reqCode");
Log.i("rc", rc + "");
long n = now/1000;
long e = end;
Log.i("now", n + "");
Log.i("end", e + "");
if (n > e)
{
Log.i("STATUS", "now > end " + n + ">" + e);
Intent intentstart = new Intent();
PendingIntent senderstart = PendingIntent.getBroadcast(context, rc, intentstart, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
am.cancel(senderstart);
}
else
{
Log.i("STATUS", "now <= end " + n + "<=" + e);
}
}
}
公共类RepeatingAlarm扩展了广播接收机{
@凌驾
公共void onReceive(上下文、意图){
Toast.makeText(上下文,“repeating_received”,Toast.LENGTH_SHORT.show();
long now=System.currentTimeMillis();
Bundle b=intent.getExtras();
long end=b.getLong(“结束时间”);
int rc=b.getInt(“需求代码”);
Log.i(“rc”,rc+);
长n=现在/1000;
长e=结束;
Log.i(“现在”,n+);
Log.i(“结束”,e+);
如果(n>e)
{
日志i(“状态”,“现在>结束”+n+“>”+e);
意向意向开始=新意向();
PendingEvent senderstart=PendingEvent.getBroadcast(上下文、rc、意图开始、PendingEvent.FLAG_CANCEL_CURRENT);
AlarmManager am=(AlarmManager)context.getSystemService(context.ALARM\u服务);
am.取消(senderstart);
}
其他的
{
Log.i(“状态”,“现在”
我不能像在活动中取消警报那样取消警报
好吧,你可以。更准确地说,我看不出有什么问题
这就是我在活动中取消警报的方式
该代码设置报警。它不会取消报警。若要取消报警,请在等效的意图上创建一个类型相同的pendingent
(例如getBroadcast()
)(除附加外的所有内容),然后调用cancel()AlarmManager上的
是的,很抱歉我键入了错误。我从Intent()中删除了参数,但ALARM\u服务无法解析为变量。我应该如何修改它?@erdomester:上下文。ALARM\u服务
。现在:对于RepeatingAlarm类型,getSystemService(String)方法未定义(这是一行:AlarmManager am=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
)@erdomester:如果您阅读了文档,您会看到这一点。此外,如果您阅读了文档,您会得到一个上下文
,作为onReceive()
的参数。因此,请调用getSystemService()
在提供的上下文上。谢谢!我编辑了这个主题。你能帮我解释一下,为什么在结束时间过后警报没有取消吗?