1

I'm quite new to Android development and I've decided to create a repeating alarm app where you'd choose when the cycle ends, like after 5 alarm bursts. I have set the alarm and all that, I have a button to cancel the alarm but I can't limit it so it stops automatically after that said amount of alarm bursts. Is there a way to do it? I want to be able to write how many bursts I want in the EditText window, write the delay between the alarms and then press the button to set it.

public class MainActivity extends AppCompatActivity {
    private TextView mTextView;
    private Double delay;
    private int howManyTimes;
    private EditText remaining;
    private EditText iterator;

I want to store the amount of bursts in howManyTimes.

My OnClickListener looks like this (iterator is an EditText where I write the number of bursts and remaining is an EditText where I write the delay between the bursts):

public void onClick(View v) {
    if (remaining.getText().toString().equals("") || remaining.getText().toString().equals(".")) {
        delay = 0.0;
    } else {
        delay = (60 * 60 * 1000) * Double.parseDouble(remaining.getText().toString());
    }
    if (iterator.getText().toString().equals("") || iterator.getText().toString().equals(".")) {
        howManyTimes = 0;
    } else {
        howManyTimes = Integer.parseInt(iterator.getText().toString());
    }
    if (howManyTimes > 0) {
        double tmpDelay = delay;
        int tmpIterator = howManyTimes;
        updateTimeText(tmpIterator, tmpDelay);
        startAlarm();
    }
}

startAlarm() looks like this:

private void startAlarm() {
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, AlertReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0);


    alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + delay.longValue(),
            delay.longValue(), pendingIntent);
}

this is my broadcast receiver:

    public class AlertReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationHelper notificationHelper = new NotificationHelper(context);
        NotificationCompat.Builder nb = notificationHelper.getChannelNotification();
        notificationHelper.getManager().notify(1, nb.build());

    }
}
  • It would help if you could include some of the code you've done so far. For example, how are you storing the number of bursts that should run? Can you post the code from where you schedule the alarm? – notquiteamonad Mar 03 '20 at 18:10

1 Answers1

1

I think one way to achieve it is to store the amount of desired bursts as well as the amount of bursts elapsed so far in persistent memory i.e. in your apps preferences. that way the information will persist even if you close your app inbetween alarms. then just add an if statement and cancel your alarm if the amount of elapsed bursts has become equal to the amount of total desired bursts. Ill add some code in a sec.

here you save the how many times var to shared prefs and reset the counter of the elapsed ones to zero.

public void onClick(View v) {
    if (remaining.getText().toString().equals("") || remaining.getText().toString().equals(".")) {
        delay = 0.0;
    } else {
        delay = (60 * 60 * 1000) * Double.parseDouble(remaining.getText().toString());
    }
    if (iterator.getText().toString().equals("") || iterator.getText().toString().equals(".")) {
        howManyTimes = 0;
    } else {
        howManyTimes = Integer.parseInt(iterator.getText().toString());
        //here you store the amount of desired bursts to persistent memory and reset elapsed bursts to 0
        SharedPreferences sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this);
            SharedPreferences.Editor editor=sharedPreferences.edit();
            editor.putInt("desiredBursts",howManyTimes);
            editor.putInt("elapseddBursts",0);
            editor.apply();
    }
    if (howManyTimes > 0) {
        double tmpDelay = delay;
        int tmpIterator = howManyTimes;
        updateTimeText(tmpIterator, tmpDelay);
        startAlarm();
    }
}

then you need to add the following code to your broadcast receiver (which you havent included in your question). it will update the amount of elapsed bursts and cancel the alarm once elapsed bursts match total desired bursts.

//this goes in your broadcastreceiver
SharedPreferences sharedPreferences= PreferenceManager.getDefaultSharedPreferences(context);
int desiredBursts = sharedPreferences.getInt("desiredBursts", 1);//this will get the desired bursts
int elapsedBursts = sharedPreferences.getInt("elapsedBursts", 0);//this will get the running burst count
if(desiredBursts>elapsedBursts){
    SharedPreferences.Editor editor=sharedPreferences.edit();
    editor.putInt("elapsedBursts",elapsedBursts+1);
    editor.apply();
    //play actual alarmsound here either with soundpool or mediaplayer
}else{
    //cancel the alarm here
}
quealegriamasalegre
  • 2,887
  • 1
  • 13
  • 35
  • Should I add this if to oncreate method? There's my issue, because the alarm works even in the background or after I destroy the app, so would this what you said solve that issue even for that? I'm a newbie, bo I don't understand how the persistent memory works in cases like that so I just ask. – Patryk Lipka Mar 03 '20 at 19:06
  • yes, its exactly to solve that issue. shared prefs are persistent memory. as long as you dont uninstall the app the counter will work – quealegriamasalegre Mar 03 '20 at 19:09
  • regarding where to add it: the onClick addendum obviously boes in the onClick methos that I assum is in onCreate. the other code goes in the BroadcastReceiver, yours is called AlarmReceiver.class – quealegriamasalegre Mar 03 '20 at 19:17
  • Updated my BroadcastReceiver, the problem I have there is that I kinda don't understand how to cancel the alarm from the AlertReceiver – Patryk Lipka Mar 03 '20 at 22:56
  • check this link https://stackoverflow.com/questions/20159649/how-to-cancel-alarm-programmatically-in-android I guess you just have to write the same intent in the receiver and then invoke the cancel method – quealegriamasalegre Mar 03 '20 at 23:16
  • AlarmManager alarmManager=((AlarmManager)context.getSystemService(Context.ALARM_SERVICE)); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, intent, 0); alarmManager.cancel(pendingIntent); – quealegriamasalegre Mar 03 '20 at 23:39
  • It did work, thank you for your solution, didn't know if I should write it as I'm new here, gave u thumb up but my reputation doesn't show it publicly – Patryk Lipka Mar 05 '20 at 06:22
  • happy to have helped. I think you can still mark it as the right solution though for other people with similar issues (just click the check sign ;)) – quealegriamasalegre Mar 05 '20 at 08:07