7

I want to repeat my task on every Monday at 09:00AM & 05:00 PM. I used following code for that but I am not able to repeat the task.

Activity Code:

public class AndroidScheduledActivity extends Activity {
    /** Called when the activity is first created. */
    int id = 115;
    Intent myIntent;
    PendingIntent pendingIntent;
    AlarmManager alarmManager;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button buttonStart = (Button)findViewById(R.id.start);

        myIntent = new Intent(getBaseContext(), MyScheduledReceiver.class);
        myIntent.putExtra("id", id);
        pendingIntent = PendingIntent.getBroadcast(getBaseContext(), id, myIntent, 0);

        alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

        buttonStart.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                setForMonday();
                finish();
            }});
    }

    public void setForMonday() {
        Calendar calendar = Calendar.getInstance();


        calendar.set(Calendar.DAY_OF_WEEK,2);
        calendar.set(Calendar.HOUR,09);
        calendar.set(Calendar.MINUTE, 00);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        System.out.println("Old is set@ :== " + calendar.getTime());


        long interval = calendar.getTimeInMillis() + 604800000L;
        System.out.println("Next Millis = " + interval);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval, pendingIntent);
    }
}

Note: I counted interval based on 86400000 * 7 = 604800000;

Receiver:

public class MyScheduledReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        System.out.println("Receiver");
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Scorpion
  • 6,831
  • 16
  • 75
  • 123
  • Just try and check monday is Day 2 or 1 I doubt on that,else Actually I dont see much any problem in your code, may be its in your testing, after setting this alarm change your device/emulator time 9AM and Date of next monday..and that will fire the alarm I am sure. – MKJParekh Jun 23 '12 at 06:47
  • I got the problem. Its in interval. In place of interval I used AlarmManager.INTERVAL_DAY * 7 in setRepeating method. – Scorpion Jun 23 '12 at 07:14

1 Answers1

12

Remove interval variable and use the following in place of that.

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY * 7, pendingIntent);
Scorpion
  • 6,831
  • 16
  • 75
  • 123
  • if you need to alarm it every monday to friday how to do it for the setRepeating method? – Kairi San Feb 12 '16 at 05:43
  • 1
    Then you to set alarm repeating every day and in on Receive check current day. If its sat or sun ignore it. – Vikram Aug 11 '16 at 11:21