问题描述
我目前正在开发一个使用Android的AlarmManager的应用程序。我正在尝试向AlarmManager添加新警报,但是经过数小时尝试各种操作并检查SO上的各种线程之后,我仍然处于困境。我的代码有任何问题吗?
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?
主要活动-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 (请参见上面的调试部分)。
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);
}
}
我还在AndroidManifest.xml中设置了< receiver android:process =:remote android:name = AlarmReceiver />
。我不知道是什么原因导致了这个问题,但是仍然在发生。任何帮助将不胜感激,在此先感谢您。
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.
编辑1
将时区更改为UTC似乎无法解决任何问题,无论如何我的日历似乎都默认为UTC。当前代码:
Edit 1Changing 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);
推荐答案
是否可能是 AlarmManager.set(AlarmManager.RTC_WAKEUP,...)
需要UTC时间吗?如果您设置本地时间并且不适应UTC,则可能会导致问题。简而言之,警报可能已正确添加,但除非您的时区为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.
从文档中...
系统中的警报时间.currentTimeMillis()( UTC的挂钟时间),它将在设备关闭时唤醒设备。
Alarm time in System.currentTimeMillis() (wall clock time in UTC), which will wake up the device when it goes off.
这篇关于AlarmManager不添加新警报,或不触发接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!