Repeating Alarm not working

2019-05-06 03:43发布

I know this type of questions are aksed so many times...but please first read my question before down voting or marking as duplicate.

I have referred so many SO questions like this for managing alarm but cant find any solution so i am here.

In my app i want to trigger repeating alarm one at daily 8.00 am and others as user specified.

First i tired to repeat alarm using setRepeating...this is my code

Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, 1);
            calendar.set(Calendar.HOUR_OF_DAY, 8);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.AM_PM,Calendar.AM);
            //calendar.set(Calendar.MILLISECOND, 0);


            AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
            Intent pintent = new Intent(this, AlarmReceiver.class);

            pintent.putExtra("id", 0);
            pintent.putExtra("ontime", false);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
                    pintent, PendingIntent.FLAG_UPDATE_CURRENT);


            am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);

Actually the problem is alarm is not firing after long sleep. If it test using changing date and time it fires perfactly....but it stops firing after a night...

So i switched to second option as mentioned in given link. I set alarm using setExact as follow and reschedule it on each fire.

 Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, 1);
            calendar.set(Calendar.HOUR_OF_DAY, 8);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.AM_PM,Calendar.AM);
            //calendar.set(Calendar.MILLISECOND, 0);


            AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
            Intent pintent = new Intent(this, AlarmReceiver.class);

            pintent.putExtra("id", 0);
            pintent.putExtra("ontime", false);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
                    pintent, PendingIntent.FLAG_UPDATE_CURRENT);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(calendar.getTimeInMillis(), pendingIntent);
                am.setAlarmClock(alarmClockInfo, pendingIntent);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                am.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
            } else
                am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

But steel i am having same issue. It is not repeating daily. Please also consider this is only the example of my default alarm. There can be N no of exact and repeating alarms as user specified.

Someone also suggest me to use service rather than alarm manager, but my service might be killed so i am afraid to use that. One other solution is to check due date and set all alarm daily, but i dont think this is the right way. Is it?

Please suggest me any other method or help me to improve my code. I badly need this to work. Thanx all. My testing devices are HTC One X (4.3), Redmi Prime(6.0) and Mi4i (5.0.2)

1条回答
贪生不怕死
2楼-- · 2019-05-06 04:42

I had many problems setting repeating alarms, but in the end this code was working on all my testing devices, maybe this helps:

    // set time
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(System.currentTimeMillis());
    c.set(Calendar.HOUR_OF_DAY, 8);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);

    long startUpTime = c.getTimeInMillis();

    // startupTime + 24 hours if alarm is in past
    if (System.currentTimeMillis() > startUpTime) {
        startUpTime = startUpTime + 24 * 60 * 60 * 1000;
    }

    // initialize alarm
    AlarmIntent alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0);

    manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, startUpTime, 24 * 60 * 60 * 1000, pendingIntent);
查看更多
登录 后发表回答