0

I tried to set a default alarm schedule. it's working for exact time as well. but always i'm calling that function trigger the alarms without set time.

This is i tried code BroadcastReceiver, pendingIntent and alarmmanager.

   public void DefaultAlert(){
    int broadcast=0;
    List<Time> times = new ArrayList<>();
    times.add(new Time(7, 00));
    times.add(new Time(9, 00));
    times.add(new Time(11, 30));
    times.add(new Time(13, 30));
    times.add(new Time(15, 00));
    times.add(new Time(17, 00));
    times.add(new Time(20, 00));
    times.add(new Time(22, 00));

    Intent intent=new Intent(MainActivity.this,MyBroadcastReceiver.class);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    for (Time time : times){
        PendingIntent pendingIntent=PendingIntent.getBroadcast(this.getApplicationContext(),broadcast++,intent,0);
        Calendar cal_alarm=Calendar.getInstance();
        cal_alarm.set(Calendar.HOUR_OF_DAY,time.hour);
        cal_alarm.set(Calendar.MINUTE,time.minute);
        cal_alarm.set(Calendar.SECOND,00);
        Log.v(TAG,"Set Alarm For : "+time.hour+":"+time.minute);

//            mOutputText.setText(TextUtils.join("\n",));
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
                alarmManager.setExact(AlarmManager.RTC_WAKEUP,cal_alarm.getTimeInMillis(),pendingIntent);
                alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis(),
                        AlarmManager.INTERVAL_DAY, pendingIntent);
            }
            if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT){
                alarmManager.set(AlarmManager.RTC_WAKEUP,cal_alarm.getTimeInMillis(),pendingIntent);
                alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis(),
                        AlarmManager.INTERVAL_DAY, pendingIntent);
            }
            mOutputText.append(broadcast+". "+time.hour+":"+time.minute+"\n");
        }
    }

what is the reason for that? and give a solution.

Thank You

  • 1
    It is kind of complicated...But this thread from earlier today might help you. [Doze Mode, Battery Optimization whitelist, AlarmManager](https://stackoverflow.com/questions/51399353/doze-mode-battery-optimization-whitelist-alarmmanager-more-frequent-than-9-min#comment89769866_51399353) – Elletlar Jul 18 '18 at 12:20
  • https://stackoverflow.com/questions/7218768/why-does-my-alarm-go-off-straight-away-android i have this type of problem? – Testing1996 Jul 19 '18 at 00:31
  • The alarms are firing immediately? That is almost always because the time is set in the past... – Elletlar Jul 19 '18 at 00:47
  • Also, there is something I don't understand about the code. Why is it setting each alarm twice? The 2nd parameter to setInexactRepeating is "time in milliseconds that the alarm should first go off, using the appropriate clock (depending on the alarm type)." But it appears that the code is also manually setting the initial time by calling set() and setExact(). – Elletlar Jul 19 '18 at 00:52
  • You haven't explained what your app does, but I doubt that setInexactRepeating is going to fulfil the requirements. You do under that as of Android 6+, the alarm may not repeat for hours while the screen is OFF?. Without knowing more about the app, I can only guess that you will probably be better served using either setAlarmClock or setExactAndAllowWhileIdle and in both cases managing the repeating logic manually since those methods do not have a repeating version. setInexactRepeating has very little utility on a modern Android device. – Elletlar Jul 19 '18 at 01:23
  • Sorry for don't mention the question correctly, actually i want to set alarm for those i mentioned times and when i set once, it should trigger every day for exact same time. when set exact time it's trigger for that time correctly, but it haven't a interval to trigger again. then i used setinexactrepeating, in that not trigger for exact time that i set, but it trigger next day. in now when i set alarms if alarm is past time on the day, it's trigger when i set the alarm immediately. can you give a solution? – Testing1996 Jul 19 '18 at 01:37
  • The APIs being used by the code will not fulfil this requirement: "it should trigger every day for exact same time". The only API that can do that is AlarmManaer.setAlarmClock. The repeating has to be handled manually by setting a new alarm each time. As far as the alarm firing immediately, try use a library called 'prettytime' to print out all the alarm times because they are in past if the alarm fires immediately. (prettytime will format System.currentTimeInMillis into strings such as "2 minutes ago") – Elletlar Jul 19 '18 at 09:16
  • @Elletlar, Thanks for your corporation :) ;) , your advice guided me. – Testing1996 Jul 19 '18 at 13:40
  • Np. Happy to help. :) I think everyone struggles with AlarmManager and its different behaviours on different versions of Android... – Elletlar Jul 19 '18 at 13:46

0 Answers0