Amin
Amin

Reputation: 1028

setAlarmClock() fires alarm repeatedly [issue turned out to be due to a Spinner]

EDIT for my solution: I was using a Spinner drop down list to set the time for the alarm and forgot that the onSelectItem method of a Spinner constantly considers itself selected, so by placing the alarm in within that method, it constantly fired. OP Below

I'm attempting to make a daily notification system. I'm using AlarmManager to push the notification at a given time, and according to the documentation, I'm using methods that should just fire an alarm once. However, the alarm is repeatedly fired until I kill the app altogether.

Here's my initialization stuff:

alarmMgr = (AlarmManager)MainActivity.this.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
        alarmIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);

alarmInfo = new AlarmManager.AlarmClockInfo(cal.getTimeInMillis(), alarmIntent);

and my AlarmReceiver class:

import static com.example.umbrellareminder.MainActivity.location_button;

public class AlarmReceiver extends BroadcastReceiver {
    private Context context;

    @RequiresApi(api = Build.VERSION_CODES.O)
    public void onReceive(Context context, Intent intent) {
        this.context = context;
        location_button.performClick();
    }
}

(location_button is what I use to run an http request and push the notification)

Now to actually fire the alarm, I've tried both

alarmMgr.setAlarmClock(alarmInfo,alarmIntent);

and

alarmMgr.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),alarmIntent);

However, they both run repeatedly. I also tried adding a time delay then including alarmIntent.cancel(), which does cancel the alarm, but it cancels the alarm before it can fire at all (which is confusing, would have figured it would do that after the time delay)

So is there any reason I'm missing why this alarm is repeatedly firing?

Editing to add additional info: I have a button that is used to make an http request/get some info to display and use for the notification. The alarm itself is set within a Spinner (the drop down list is a list of optional times for the alarm), in the onItemSelectedListener like so:

Spinner time_list = findViewById(R.id.time_list);
        time_list.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                remind_time = time_list.getSelectedItem().toString();

                int hour = Integer.parseInt(Character.toString(remind_time.charAt(0)));
                int minute = Integer.parseInt(remind_time.substring(2,3));

                Calendar cal = Calendar.getInstance();
                cal.setTimeInMillis(System.currentTimeMillis());
                cal.set(Calendar.HOUR_OF_DAY, 9);
                cal.set(Calendar.MINUTE, 25);
                cal.set(Calendar.SECOND, 45);

                if(cal.getTimeInMillis() <= System.currentTimeMillis()){
                    cal.add(Calendar.DAY_OF_MONTH, 1);
                }

                Log.d("TIME",cal.getTime().toString());
                alarmInfo = new AlarmManager.AlarmClockInfo(cal.getTimeInMillis(), alarmIntent);

                //line where I set the alarm
                alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY,alarmIntent);
            }

Upvotes: 0

Views: 119

Answers (0)

Related Questions