Question

Below is my code for setting repeat alarm for daily. Now i need to stop my alarm after 2 days. I tried to stop alarm after adding 2 days but it is not working . Please anyone help me.

Intent myIntent = new Intent(this, MyAlarmService.class);

    pendingIntent = PendingIntent.getService(this, (int) System.currentTimeMillis(), myIntent, 0); 

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, hour1);
    calendar.set(Calendar.MINUTE, min1);
    calendar.set(Calendar.SECOND, 0);   

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, pendingIntent);

    Toast.makeText(this, "Start Daily Alarm", Toast.LENGTH_LONG).show(); 

     //trying to cancel after 2 days
    // add days to current date using Calendar.add method 

    calendar.add(Calendar.DATE, 2);       
    pendingIntent = PendingIntent.getService(this, (int) System.currentTimeMillis(), myIntent, 0);
    AlarmManager alarmManagerstop = (AlarmManager)getSystemService(ALARM_SERVICE);
    alarmManagerstop.cancel(pendingIntent);     

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top