Set Repeat days of week alarm in android

后端 未结 4 1890
别跟我提以往
别跟我提以往 2020-12-12 23:08

Can somebody give good logic for set repeat days of week alarm? I have done weekly Alarm by using

            alarmCalendar.set(Calendar.HOUR, AlarmHrsInInt         


        
4条回答
  •  渐次进展
    2020-12-12 23:40

    To set repeat alarm for week days,use below code. Hope this is helpful.

            Calendar calender= Calendar.getInstance();
    
            calender.set(Calendar.DAY_OF_WEEK, weekNo);  //here pass week number
            calender.set(Calendar.HOUR_OF_DAY, hour);  //pass hour which you have select
            calender.set(Calendar.MINUTE, min);  //pass min which you have select
            calender.set(Calendar.SECOND, 0);
            calender.set(Calendar.MILLISECOND, 0);
    
            Calendar now = Calendar.getInstance();
            now.set(Calendar.SECOND, 0);
            now.set(Calendar.MILLISECOND, 0);
    
            if (calender.before(now)) {    //this condition is used for future reminder that means your reminder not fire for past time
                calender.add(Calendar.DATE, 7);
            }
    
            final int _id = (int) System.currentTimeMillis();  //this id is used to set multiple alarms
    
            Intent intent = new Intent(activity, YourServiceClass.class);
    
            PendingIntent pendingIntent = PendingIntent.getBroadcast(activity, _id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(), 7 * 24 * 60 * 60 * 1000, pendingIntent);
    

提交回复
热议问题