Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

android - How to implement yearly and monthly repeating alarms?

I want to set monthly and yearly alarm in my app.I did this for weekly. AlarmManager.INTERVAL_DAY helped me for that.But i could not find good way to implement monthly and yearly repeat.

Searched so far:

http://www.satyakomatineni.com/akc/display?url=displaynoteimpurl&ownerUserId=satya&reportId=3503

http://groups.google.com/group/android-developers/browse_thread/thread/9f946e40307073c4?pli=1

Is any other way available to do this? Any help appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think that, you have two inherent issues with this approach:

  • AlarmManager will not accept large time intervals because the number of millis will overflow the argument

  • I do not think Alarms will survive reboots of your phone that will most certainly happen during such a long period of time.

I advice that you store each alarm in a safe place and use a combination of AlarmManager and onBoot receivers to check if one of the alarms from your list must be fired this very day and just reschedule an alarm to wake you up tomorrow if none does.

public class AlarmService extends Service {
  //compat to support older devices
  @Override
  public void onStart(Intent intent, int startId) {
      onStartCommand(intent, 0, startId);
  }


  @Override
  public int onStartCommand (Intent intent, int flags, int startId){
   //your method to check if an alarm must be fired today
   checkForTodayAlarmsAndBehaveAppropriately();

   //reschedule me to check again tomorrow
    Intent serviceIntent = new Intent(AlarmService.this,AlarmService.class);
    PendingIntent restartServiceIntent = PendingIntent.getService(AlarmService.this, 0, serviceIntent,0);
    AlarmManager alarms = (AlarmManager)getSystemService(ALARM_SERVICE);
    // cancel previous alarm
    alarms.cancel(restartServiceIntent);
    // schedule alarm for today + 1 day
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DATE, 1);

    // schedule the alarm
    alarms.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), restartServiceIntent);
  }

}

To start your service at boot time use this :

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class serviceAutoLauncher  extends BroadcastReceiver{

@Override
  public void onReceive(Context context, Intent intent) {
      Intent serviceIntent = new Intent(context,AlarmService.class);
      context.startService(serviceIntent);
  }

}

Finally add this to your manifest to schedule your serviceAutoLauncher to be launched at each boot:

    <receiver android:name="serviceAutoLauncher">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"></action>
            <category android:name="android.intent.category.HOME"></category>
        </intent-filter>
    </receiver>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...