setRepeating在Alarm Manager中不起作用

时间:2018-01-30 08:23:13

标签: java android android-intent calendar alarmmanager

我使用下面的代码在android中启动和停止服务,首先重复警报工作,但第二次失败大部分时间。当我禁用第一个重复时,第二个重复。可能是什么问题?

public void startAlarm(Context context,int StartHour,int StartMin,int PauseHour,int PauseMin) {

    AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);


    Calendar firstTurn = Calendar.getInstance();
    Calendar secondTurn = Calendar.getInstance();

    // set times
    Intent intent1 = new Intent(context, TimerAlarmReciver.class);
    intent1.setAction("START");
    firstTurn.set(Calendar.HOUR_OF_DAY, StartHour);
    firstTurn.set(Calendar.MINUTE, StartMin);
    PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 15, intent1, PendingIntent.FLAG_CANCEL_CURRENT);

    Intent intent2 = new Intent(context, TimerAlarmReciver.class);
    intent2.setAction("STOP");
    secondTurn.set(Calendar.HOUR_OF_DAY, PauseHour);
    secondTurn.set(Calendar.MINUTE, PauseMin);
    PendingIntent alarmIntent2 = PendingIntent.getBroadcast(context, 16, intent2, PendingIntent.FLAG_CANCEL_CURRENT);


    alarmMgr.cancel(alarmIntent);
    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, firstTurn.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, secondTurn.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent2);
}

1 个答案:

答案 0 :(得分:0)

new AlaramTask1(context,StartHour,StartMin).excute();

new AlaramTask2(context,PauseHour,PauseMin).excute();

您可以在 onPostExecute

上执行主要逻辑