Friday, June 22, 2012

Re: [android-developers] Repeating alarm on specific day of the week

Hi Mark,

I also need to develop same kind of functionality. I need to repeat my alarm on every Monday at 09:00AM. I used the following code for that but I am unable to repeat the alarms. Can you please help me that where I am making mistake.


Activity :-


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);
   
}
}

Receiver :-

public class MyScheduledReceiver extends BroadcastReceiver {

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

       
System.out.println("Receiver");
   
}

}
On Friday, 5 March 2010 00:36:31 UTC+5:30, Mark Murphy wrote:
Open wrote:
> Can someone get me started on how to set a repeating alarm so that it
> fires at a specific time on a specific day of the week.  For example,
> I would like an alarm to trigger an intent every Monday at 8am.
>
> Looks like a lot of apps do this (e.g., Locale, Alarm Clock, etc.)

Step #1: Figure out the time you want the alarm to next be fired, in
milliseconds since the epoch (i.e., System.currentTimeMillis() timebase)

Step #2: Figure out the period in between the alarms in milliseconds

Step #3: Use AlarmManager to register a RTC or RTC_WAKEUP alarm that
will issue a PendingIntent using those two values

--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy

_Android Programming Tutorials_ Version 2.0 Available!

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment