【问题标题】:AlarmManager not adding new Alarms, or not triggering receiverAlarmManager 未添加新警报,或未触发接收器
【发布时间】:2011-08-29 20:36:31
【问题描述】:

我目前正在开发一个使用 Android 的 AlarmManager 的应用程序。我正在尝试向 AlarmManager 添加一个新警报,但经过几个小时的尝试各种事情并检查 SO 上的各种线程后,我仍然在砖墙上。我的代码有问题吗?


主要活动 - saveAlarm() 函数

/**
 * Saves the current alarm. Adds to the database if it doesn't already exist, or updates if it does.
 * Also sets alert with AlarmManager.
 */
public void saveAlarm() {
    // Create Database instance
    DbHandler db = new DbHandler(getApplicationContext());

    if(alarm.getId() == -1) {
        // Saving a new alarm
        db.open();
        alarm.setId(db.addAlarm(alarm));
        db.close();
    }
    else {
        db.open();
        db.updateAlarm(alarm);
        db.close();
    }

    // Create the wakeup intent
    Intent intent = new Intent(this, AlarmReceiver.class);
    intent.putExtra("alarm_id", alarm.getId());

    // Create the Pending Intent
    PendingIntent sender = PendingIntent.getBroadcast(this, AlarmPlayer.REQUEST_ALARM + alarm.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);

    // Debug
    Calendar now = Calendar.getInstance();
    long dTime  = alarm.getNextAlarmTime().getTimeInMillis() - now.getTimeInMillis();
    Log.d(TAG, "Setting alarm for " + (dTime / 1000) + " seconds time");

    // Add to Android Alarm Manager
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, alarm.getNextAlarmTime().getTimeInMillis(), sender);    
}

我已确认将正确的时间传递到 am.set(请参阅上面的调试部分)。


AlarmReceiver.class

/**
* This class listens out for broadcasts from AlarmManager
* and launches the AlarmPlayer activity accordingly.
* @author Michael
*
*/
public class AlarmReceiver extends BroadcastReceiver {

     @Override
    public void onReceive(Context c, Intent intent) {
        Log.d("RSS Alarm", "Waking up alarm");
            // Launch the AlarmPlayer activity
        Intent i = new Intent(c, AlarmPlayer.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        c.startActivity(i);
    }

}

我还在 AndroidManifest.xml 中设置了<receiver android:process=":remote" android:name="AlarmReceiver" />。我不知道是什么导致了这个问题,但它仍然在发生。任何帮助将不胜感激,非常感谢提前。


编辑 1 将时区更改为 UTC 似乎无法解决任何问题,无论如何,我的日历似乎默认为 UTC。当前代码:

// Debug
Calendar now = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
long dTime  = alarm.getNextAlarmTime().getTimeInMillis() - now.getTimeInMillis();
Log.d(TAG, "Setting alarm for " + (dTime / 1000) + " seconds time");

// Add to Android Alarm Manager
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Log.d(TAG, "Timezone offset is " + TimeZone.getDefault().getRawOffset());
Log.d(TAG, "UTC time is currently " + Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTimeInMillis() / 1000);
am.set(AlarmManager.RTC_WAKEUP, (alarm.getNextAlarmTime().getTimeInMillis() - TimeZone.getDefault().getRawOffset()), sender);

【问题讨论】:

    标签: java android alarmmanager


    【解决方案1】:

    这可能是AlarmManager.set(AlarmManager.RTC_WAKEUP, ...) 需要UTC 时间的情况吗?如果您设置“本地时间”并且不适应 UTC,这会导致问题。简而言之,警报可能已正确添加,但除非您的时区是 UTC,否则它不会在您期望的时候触发。

    来自文档...

    public static final int RTC_WAKEUP 自:API 级别 1

    System.currentTimeMillis() 中的闹钟时间(UTC 挂钟时间),它会在设备关闭时唤醒设备。

    【讨论】:

    • 恐怕不行。事实证明,无论如何,我的日历默认为 UTC(使用下面编辑 1 中的代码进行调试会给出消息“时区偏移量为 0”)。虽然这是你提出的一个好点(谢谢),但它并不能解决这个问题。
    • 好的,值得一试,因为我在这里看到了一些与 UTC 要求相关的问题。我唯一能想到(并且应该提到)的另一件事是考虑夏令时 - 语言环境的时区偏移量是恒定的,并且不会随 DST 变化而变化。如果这不是罪魁祸首,那么我想不出别的了,抱歉。
    • 是的,原来是时间计算问题。刚刚重新编写了AlarmManager.set(.. 部分,现在一切正常。干杯。
    【解决方案2】:

    在您的清单中,我相信您需要 android:name".AlarmReceiver" 注意点。 如果不这样做,请发布您的清单文件。另外,您的 logcat 是否提到添加警报和警报触发?

    编辑:你可以试试这个 AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    除此之外,我没有看到任何东西向我扑来。祝你好运

    【讨论】:

    • 这是这个问题更奇怪的方面之一 - 除了我在代码中留下的日志注释之外,没有提到 AlarmManager 或任何类似的东西。现在将发布清单。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    相关资源
    最近更新 更多