how to repeat alarm after 1 day in android

那年仲夏 提交于 2019-12-04 17:04:40
Maneesh

Hopefully below code will help, I used the same in my app. Here the argument passed in AlarmManager class for repeating should be 24*60*60*1000

AlarmManager am = (AlarmManager) ct.getSystemService(Context.ALARM_SERVICE);         
Intent intent1 = new Intent(ct, TimeAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ct, 0,intent1, PendingIntent.FLAG_CANCEL_CURRENT);

Date curr=new Date();
curr.setHours(h);
curr.setMinutes(m);
c.setTime(curr);
c.set(Calendar.SECOND, 0);

Calendar c1 = Calendar.getInstance();
am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),24*60*60*1000, pendingIntent);

It is better to use inexactrepeating instead of setExactRepeating keeping in mind of battery consumption as per android documentation.

      public void scheduleAlarms(AlarmManager mgr, PendingIntent pi, Context context) 
                {

                    // every day at scheduled time 
                    Calendar calendar = Calendar.getInstance();
                   // if it's after or equal 9 am schedule for next day
                   if (Calendar.getInstance().get(Calendar.HOUR_OF_DAY) >= 9) {
                        calendar.add(Calendar.DAY_OF_YEAR, 1); 
                    }
                    calendar.set(Calendar.HOUR_OF_DAY, 9);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);

              mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
    }

Try this

AlarmManager am = (AlarmManager) ct.getSystemService(Context.ALARM_SERVICE);         
Intent intent1 = new Intent(ct, TimeAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ct, 0,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, yourtime,AlarmManager.INTERVAL_DAY, pendingIntent);
Pratibha Sarode
// Retrieve a PendingIntent that will perform a broadcast
Intent alarmIntent = new Intent(HomeContactActivity.this,
        AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(
        HomeContactActivity.this, 0, alarmIntent, 0);

AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

// Set the alarm to start at 10:00 AM
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);




manager.setRepeating(AlarmManager.RTC_WAKEUP,
        calendar.getTimeInMillis(), 86400000, // for repeating in every 24 hours
        pendingIntent);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!