1

这是我关于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>

这是我的警报接收器:

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

提前非常感谢!

4

0 回答 0