android repeat alarm for every monday or every tuesday

前端 未结 1 2000
无人及你
无人及你 2020-12-11 12:10

I am developing an alarm based application, in which i have to repeat the alarm for every weekday like (every monday, tuesday, wednesday) based on user input. I used this sn

相关标签:
1条回答
  • 2020-12-11 12:55

    here is my code to set alarm to repeat every month in 4th

    public void init(Context context) {
    
        Intent intent = new Intent(context, AlarmReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(context, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
    
        // Get the AlarmManager service
        alarmManager = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
    }   
    public void createAlarmControler(Context context) {
    
        init(context);
        Calendar cal = Calendar.getInstance();
    
        cal.set(Calendar.DAY_OF_MONTH, 4);
        cal.set(Calendar.HOUR_OF_DAY, 10);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        Date now = new Date(System.currentTimeMillis());
        if (cal.getTime().before(now)) {
            cal.add(Calendar.MONTH, 1);
        }
        long firstTime = cal.getTime().getTime();
    
        alarmManager.cancel(pendingIntent);
        Log.e("", "firstTime: " + firstTime);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, firstTime, 1000 * 60
                * 60 * 24 * 30, pendingIntent);
    }
    
    public void cancelAlarmControler(Context context) {
    
        init(context);
    
        alarmManager.cancel(pendingIntent);
    }
    
    0 讨论(0)
提交回复
热议问题