3

i am trying to make an alarm app which triggers event at specific time. it shows alarm for 3 times for the specific time. after showing 3 times, it stops. here is my alarm setter class.

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

}

manifest permissions for alarm:

<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 code:

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

here is my alarm activity codes related to next alarm setting for snooze:

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

so, issue again is : the alarm fires fine for the first time. even if the app is in background. but, the alarm doesnt always fire for snooze alarms. sometimes, it does. sometimes it doesnt.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Star Lut
  • 416
  • 6
  • 11

0 Answers0