Android如何设置可靠的长效报警?

How to set a reliable long-term alarm in Android?

我正在使用 Phonegap 开发 Android 应用程序。我需要设置一个长期闹钟来提醒用户一些事情,比如每月的任务。

我正在为此搜索插件,但我发现最流行的插件没有足够的可靠性。我试过使用它,但它只适用于短期警报,而且并不总是有效。

我问了开发者,他说不知道为什么会失败。

在这种情况下,我打算开发自己的插件来帮助我解决这个问题。

我对 Android 本机的体验一般,但我之前有一个问题:

¿如何在Android上设置这种可靠性高的长期警报,最好是不用服务,而且最简单的方法是什么?可能吗?

当然,我想保留警报,即使手机重启,应用程序被杀死等等。

PD:谢谢,对不起我的英语水平。

不知道,你测试的是哪个插件,因为你没说。

Katzer 的这个插件是您安排提醒所需要的: https://github.com/katzer/cordova-plugin-local-notifications/

这是计划的本地通知的示例:

window.plugin.notification.local.add({
    id:         String,  // A unique id of the notification
    date:       Date,    // This expects a date object
    message:    String,  // The message that is displayed
    title:      String,  // The title of the message
    repeat:     String,  // Either 'secondly', 'minutely', 'hourly', 'daily', 'weekly', 'monthly' or 'yearly'
    badge:      Number,  // Displays number badge to notification
    sound:      String,  // A sound to be played
    json:       String,  // Data to be passed through the notification
    autoCancel: Boolean, // Setting this flag and the notification is automatically cancelled when the user clicks it
    ongoing:    Boolean, // Prevent clearing of notification (Android only)
}, callback, scope);

这就是您安排未来日期通知的方式:

var now                  = new Date().getTime(),
    _60_seconds_from_now = new Date(now + 60*1000);

window.plugin.notification.local.add({
    id:      1,
    title:   'Reminder',
    message: 'Dont forget to buy some flowers.',
    repeat:  'weekly',
    date:    _60_seconds_from_now
});

实际时间可以用这个脚本计算:

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();

if(dd<10) {
    dd='0'+dd
} 

if(mm<10) {
    mm='0'+mm
} 

today = mm+'/'+dd+'/'+yyyy;
document.write(today);

这应该对您有所帮助。

Katzer在新版本中重写了所有插件,最近测试了一下,感觉运行良好。