Android:一次性警报反复触发

时间:2016-08-08 07:52:07

标签: android alarmmanager alarm

我在应用程序上工作,用户可以存储事件并将其设置为“重复时间” (例如 - 每个星期一我都有足球训练,所以我把活动放到应用程序中,它让我想起每个星期一我都有训练 - 是的,就像提醒一样)

事件类仅包含title,startTime,repeatTime和requestCode(警报的唯一编号..)。

我决定使用AlarmManager来安排闹钟,所以当闹钟响起时它应该只做一件事 - 检查Event是否应该重复 - 如果不是只是从数据库中删除事件,如果是,则创建新事件,存储到数据库并设置新事件该事件的警报

一切“工作正常”,我的意思是删除或存储到数据库也设置新警报正在工作但是问题是当第一次警报触发时它就是无法停止,首先报警创建新的警报和新警报是火灾,它会创建另一个警报,触发并创建另一个警报......就像在循环中一样......

那有什么问题呢?在BroadcastReciever中创建另一个警报不是一个好习惯吗?或者是我设置闹钟的坏事?我只想设置只发一次的NO-REPEAT报警,做一件事就可以了。我试图在几乎所有地方找到解决方案,但我无法找到它:/

这是我的AlarmReciever

    public class AlarmReceiver extends BroadcastReceiver {

    private static final String ALARM_RECIVER_TAG = "AlarmReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null && intent.getStringExtra("starttime") == null) {
            Log.i(ALARM_RECIVER_TAG, "Alarm not recieve properly");
        } else {
            handleEvents(context,intent.getLongExtra("starttime",0));
            Log.i(ALARM_RECIVER_TAG, "Alarm recieve");
        }
    }

    public void handleEvents(Context context, Long startTime) {
        DBHelper dbHelper = DBHelper.getInstance(context);
        Event event = dbHelper.getEvent(new Timestamp(startTime));
        if (event == null) return; //should not happenned

        if (event.getRepeatTime().equalsIgnoreCase("no repeat")) {
            dbHelper.deleteEvent(context,event.getStartTime().getTime());
            return;
        }

        Event newEvent = new Event();
        newEvent.setEventTitle(event.getEventTitle());
        newEvent.setRepeatTime(event.getRepeatTime());
        newEvent.setStartTime(CommonUtils.getDateByRepeat(event.getRepeatTime(),event.getStartTime()));

        dbHelper.deleteEvent(context,event.getStartTime().getTime());
        try {
            dbHelper.putInformation(dbHelper,newEvent);
            Log.i(ALARM_RECIVER_TAG,"everything ok");
        } catch (Exception e) {
            Log.i(ALARM_RECIVER_TAG,"something is bad");
            e.printStackTrace();
        }

        if (newEvent.getStartTime() != null) {
            setRecurringAlarm(context,newEvent);
        }

    }

    public void setRecurringAlarm(Context context, Event event) {
        // set time when alarm should fire
        Calendar updateTime = Calendar.getInstance();
        updateTime.set(Calendar.HOUR_OF_DAY, event.getSendingTime().getHours());
        updateTime.set(Calendar.MINUTE, event.getSendingTime().getMinutes());
        updateTime.set(Calendar.SECOND, event.getSendingTime().getSeconds());

        Intent intent = new Intent(context, AlarmReceiver.class);
        intent.putExtra("starttime",event.getStartTime().getTime());
        PendingIntent alarmIntent = PendingIntent.getBroadcast(context, event.getRequestCode(), intent, PendingIntent.FLAG_ONE_SHOT);
        AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
      alarms.set(AlarmManager.RTC_WAKEUP,updateTime.getTimeInMillis(),alarmIntent);
    }
}

以下是我如何调用来自活动

的警报
 AlarmReceiver alarmReceiver = new AlarmReceiver();
    alarmReceiver.setRecurringAlarm(context, event);

0 个答案:

没有答案