AlarmManager 未添加新警报,或未触发接收器

发布于 2024-12-01 18:16:02 字数 2996 浏览 1 评论 0原文

我目前正在开发一个使用 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 中设置了 。我不知道是什么导致了这个问题,但它仍然发生了。任何帮助将不胜感激,非常感谢。


编辑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);

I'm currently developing an application which makes use of Android's AlarmManager. I'm trying to add a new alarm to the AlarmManager, but after several hours of trying various things and checking various threads on SO, I'm still at a brick wall. Are there any problems with my code?


Main Activity - saveAlarm() function

/**
 * 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);    
}

I've verified that the correct time is being passed into am.set (see the debug section above it).


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);
    }

}

I also have <receiver android:process=":remote" android:name="AlarmReceiver" /> set up in AndroidManifest.xml. I have no idea what's causing this problem, but it's happening nonetheless. Any help would be greatly appreciated, many thanks in advance.


Edit 1
Changing the timezone to UTC doesn't seem to solve anything, my calendar seems to default to UTC regardless. Current code:

// 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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妄司 2024-12-08 18:16:02

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

从文档中...

public static final int RTC_WAKEUP 自:API 级别 1

System.currentTimeMillis() 中的闹钟时间(UTC 挂钟时间),当设备响起时会唤醒设备。

Is this possibly a case that AlarmManager.set(AlarmManager.RTC_WAKEUP, ...) needs the time in UTC? This causes problems if you're setting a 'local time' and don't adjust to UTC. In short, the alarm may be being properly added but it doesn't trigger when you expect it unless your time-zone is UTC.

From the docs...

public static final int RTC_WAKEUP Since: API Level 1

Alarm time in System.currentTimeMillis() (wall clock time in UTC), which will wake up the device when it goes off.

薄情伤 2024-12-08 18:16:02

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

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

除此之外,我没有看到任何事情让我惊讶。祝你好运

In your manifest, I believe you need android:name".AlarmReceiver" note the dot.
If that doesn't do it, post your manifest file. Also, does your logcat mention adding alarm and alarm triggering?

Edit: You might try this
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

Other than that, I don't see anything jumping out at me. Good Luck

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文