Alarmmanager始终不触发未决意图

时间:2018-09-06 07:29:10

标签: android alarmmanager repeatingalarm

我正在尝试制作一个可在特定时间触发事件的警报应用程序。它在特定时间显示3次警报。显示3次后,它停止。 这是我的警报设置器类。

public class AlarmUtil {
private Context context;
private Intent intent;

public AlarmUtil(Context context) {
    this.context = context;
    intent = new Intent(context, AlarmReceiver.class);
}

private void setAlarm(long time, int alarmId){
    Log.d("alarmUtils" , "setAlarm: " + time);
    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);


    PendingIntent pendingIntent = PendingIntent.getBroadcast(context , alarmId , intent, PendingIntent.FLAG_UPDATE_CURRENT);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time, pendingIntent);
    }else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent);
    } else {
        alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
    }
}


public void cancelAlarm(int alarmID){
    AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, alarmID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    manager.cancel(pendingIntent);
}

public void setSnoozeAlarm(int alarmID , long time , int snoozeCounter){
    intent = new Intent(context, AlarmReceiver.class);
    intent.putExtra(ValueConstants.ALARM_ID, alarmID);
    intent.putExtra(ValueConstants.SNOOZE_COUNTER, snoozeCounter + 1);
    setAlarm(time, alarmID);
}

public void alarmSetter(AlarmDB alarmDB){
    intent.putExtra(ValueConstants.ALARM_ID, alarmDB.getId().intValue());
    intent.putExtra(ValueConstants.SNOOZE_COUNTER, 0);
    long time = new TimeFormater().amPmtoMillies(alarmDB.alarTime);
    setAlarm(time, alarmDB.getId().intValue());
}

}

警报的清单权限:

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.SET_ALARM" />

接收方代码:

<receiver android:name=".receivers.AlarmReceiver"
        android:enabled="true"/>

这是我的与下一个贪睡警报设置有关的警报活动代码:

Bundle extras = getIntent().getExtras();
    if (extras!=null){
        alarmID = extras.getInt(ValueConstants.ALARM_ID);
        notificationFlag= extras.getBoolean(ValueConstants.NOTIFICATION_FLAG);
        snoozeCounter = extras.getInt(ValueConstants.SNOOZE_COUNTER);

        Log.d("alarmID" , "alarmActivity " + alarmID);
    }


private void initSnoozeAlarm(){
    AlarmUtil alarmUtil = new AlarmUtil(getApplication());
    alarmUtil.setSnoozeAlarm(alarmID, System.currentTimeMillis()+60000, snoozeCounter);
    Log.d("alarmActivity" , "snoozealarm :" + snoozeCounter + " " +mPrefs.getSnoozeCounter());
    finish();
}

private void cancelSnoozeAlarm(){
    AlarmUtil alarmUtils = new AlarmUtil(this);
    alarmUtils.cancelAlarm(alarmID);
}

//cancel timer so it doesnt set another snoozealarm, set notification flag, cancel notification, set snooze alarm or notify not done
@OnClick(R.id.noBtn)
public void negetiveAnswer() {
    if(countDownTimer != null){
        countDownTimer.cancel();
    }
    notificationFlag = true;
    cancelNotification();
    if (snoozeCounter >= 2){
        notifyNotDone();
        finish();
    } else {
        initSnoozeAlarm();
    }
}

所以,再次出现的问题是: 警报第一次响起。即使该应用程序在后台运行。 但是,闹铃不会总是因贪睡而触发。有时候,确实如此。有时没有。

0 个答案:

没有答案