这是我关于stackoverflow的第一个问题。我已经阅读了很多类似的主题,但一直无法找到问题而且我有点绝望了。
我开发了一个带闹钟的应用程序。此闹钟仅在某些情况下响铃。如果它们不适用,我会向用户发送通知,并在10分钟后使用setAlarmClock()
设置下一个警报。如果条件在用户指定的时间之前不适用,则警报永远不会消失。
现在,当我离开手机一段时间未检查并查看通知时,我发现它们并非每10分钟进入一次,但非常不规律。有时它会在10分钟后出现,有时则是11,15,30或其他任何时间。
问题只发生在未插入手机时,因此我认为它与Android的打盹模式有关。 setAlarmClock()
应该帮助解决这个问题,但事实并非如此。我也试过了setExactAndAllowWhileIdle()
,但这也没用。因此,如果有人知道问题可能是什么,我会非常感激地听到它。
这是我的代码: 首先,我的AlarmController中的两个方法
//this is called the first time an alarm is set
public void registerAlarm(int id, AlarmClock alarm) {
if (alarmManager == null)
alarmManager = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
Calendar alarmTime = alarm.getAlarmCalendar();
//create alarm intent
Intent intent = new Intent("START_ALARM");
intent.putExtra("alarm_id",id);
PendingIntent alarmIntent = PendingIntent.getBroadcast(ctx, id, intent, 0);
AlarmManager.AlarmClockInfo info = new AlarmManager.AlarmClockInfo(alarmTime.getTimeInMillis(),alarmIntent);
alarmManager.setAlarmClock(info, alarmIntent);
//alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,alarmTime.getTimeInMillis(),alarmIntent);
}
// This is called if the conditions didn't apply
public void registerSnoozeAlarm(int id, int minutes) {
if (alarmManager == null)
alarmManager = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
long alarmTime = System.currentTimeMillis() + 1000*60*minutes;
//create alarm intent
Intent intent = new Intent("START_ALARM");
intent.putExtra("alarm_id",id);
PendingIntent alarmIntent = PendingIntent.getBroadcast(ctx, id, intent, 0);
AlarmManager.AlarmClockInfo info = new AlarmManager.AlarmClockInfo(alarmTime, alarmIntent);
alarmManager.setAlarmClock(info, alarmIntent);
//alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmTime,alarmIntent);
}
清单中的接收者:
<receiver android:name="packagename.AlarmReceiver"
android:exported="false">
<intent-filter>
<action android:name="START_ALARM" >
</action>
</intent-filter>
</receiver>
这是我的AlarmReceiver:
public class AlarmReceiver extends WakefulBroadcastReceiver implements Observer{
private Context context;
private AlarmClock alarm;
private Intent intent;
@Override
public void onReceive(Context context, Intent intent) {
this.context = context;
this.intent = intent;
//request data from server
Bundle extras = intent.getExtras();
int alarmID = extras.getInt("alarm_id");
alarm = DataHandler.getInstance(context).getAlarm(alarmID);
DataHandler.getInstance(context).requestDataForAlarm(alarm,this);
}
@Override
public void update(Observable observable, Object data) {
Measurement measurement = (Measurement) data;
if (*someConditionapplies*){
//play ringtone
AlarmSoundController sc = AlarmSoundController.getInstance(context);
sc.playSound(alarm.getAlarmSoundPath());
//send a notification message
ComponentName comp = new ComponentName(context.getPackageName(), AlarmNotificationService.class.getName());
intent.putExtra("alarm_id",alarm.getId());
intent.putExtra("description", alarm.getDescription());
intent.putExtra("data", measurement.getData());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
else {
//if user specified end time is reached, send final notification, else try again in 10 minutes
Calendar alarmEndTime = alarm.getAlarmEndCalendar();
Calendar now = Calendar.getInstance();
if (now.before(alarmEndTime)) {
//schedule new alarm in 10 minutes
DataHandler.getInstance(context).registerSnoozeAlarm(alarm.getId(),10);
sendNoAlarmNotification(measurement, false);
setResultCode(Activity.RESULT_OK);
}
else{
//send the last notification message
sendNoAlarmNotification(measurement, true);
setResultCode(Activity.RESULT_OK);
}
}
DataHandler.getInstance(context).removeObserver(this);
}
private void sendNoAlarmNotification(Measurement measurement, boolean lastNotification) {
ComponentName comp = new ComponentName(context.getPackageName(), NoAlarmNotificationService.class.getName());
intent.putExtra("alarm_id", alarm.getId());
intent.putExtra("description", alarm.getDescription());
intent.putExtra("wind", measurement.windAvg);
intent.putExtra("last_notification", lastNotification);
intent.setComponent(comp);
startWakefulService(context, intent);
}
}
提前多多感谢!