gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:40:46 26 4
gpt4 key购买 nike

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

<小时/>

主要 Activity - 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);
}

}
<小时/>

我还有<receiver android:process=":remote" android:name="AlarmReceiver" />在 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);

最佳答案

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

来自文档...

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.

关于java - AlarmManager 未添加新警报,或未触发接收器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7235666/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com