android 设置每天重复通知,PhoneGap Android中每天都会重复本地通知

我自己从未使用过这个插件,但是对代码进行一点挖掘就会向我显示,只要你将repeatDaily设置为true,你的通知就会每天都在那里.

如果您查看AlarmHelper类,您可以看到该参数设置的if子句每天都重复.

final AlarmManager am = getAlarmManager();

...

if (repeatDaily) {

am.setRepeating(AlarmManager.RTC_WAKEUP,triggerTime,AlarmManager.INTERVAL_DAY,sender);

} else {

am.set(AlarmManager.RTC_WAKEUP,sender);

}

在AlarmReceiver课程中解释的一个额外细节是,如果您设置上一次的时间,(例如现在是11:00,并且您将警报设置为每天在08:00重复),它将立即触发,然后在第二天预定的时间.所以该类有一个if子句来防止这种情况发生.

if (currentHour != alarmHour && currentMin != alarmMin) {

/*

* If you set a repeating alarm at 11:00 in the morning and it

* should trigger every morning at 08:00 o'clock,it will

* immediately fire. E.g. Android tries to make up for the

* 'forgotten' reminder for that day. Therefore we ignore the event

* if Android tries to 'catch up'.

*/

Log.d(LocalNotification.PLUGIN_NAME,"AlarmReceiver,ignoring alarm since it is due");

return;

}

要设置日期,请使用日期参数.在您的示例中,您使用的是新的Date(),它默认返回当前日期时间,并且您的通知将每天同时显示.如果要为警报指定不同的时间,请使用所需的时间传入日期对象!

编辑

确保代码只运行一次的简单方法是使用localstorage.

function onDeviceReady(){

...

//note that this will return true if there is anything stored on "isAlarmSet"

var isSet = Boolean(window.localStorage.getItem("isAlarmSet"));

if (isSet){

//Alarm is not set,so we set it here

window.localStorage.setItem("isAlarmSet",1);

}

}

如果您取消设置闹钟,请务必清除变量:

localStorage.removeItem("isAlarmSet);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值