开发者

android alarmmanager starting once a month

I want the alarmManager to perform a task every 5th of the month at 3pm. How would I do that?

  • first start depends on when the user installs the app. Okay I can do some math calculating how many ms it takes to the next 5th of the month, but I hope there is an easier way

  • the repeat option gives me a real headache. I only can set one fixed intervall. but the time between two month is not fixed since s开发者_Go百科ometimes I have 30, sometimes 31 days (not to mention Feb).

How would I do this best?

Regards, A.


You can do this much simpler, have a look at the Date class: http://developer.android.com/reference/java/util/Date.html

And Try something like, Pseudo:

// This month 5th
Date date = new Date();
date.setDay(5);
// Next month the 5th
Date nextDate = new Date();
nextDate.setDay(5);
nextDate.setMonth(date.getMonth()+1);

You would have issues when you come to end of the year, but it's less issues than you have at the moment.

There are other Date API's and some will fix the year problem for you. Just have a look around.

EDIT

Just noticed that's deprecated use Calendar:

http://developer.android.com/reference/java/util/Calendar.html

Calendar now = Calendar.getInstance();
now.setField(Calendar.DAY_OF_MONTH, 5);
// Next month the 5th
Calendar nextDate = new = Calendar.getInstance();
nextDate.setField(Calendar.DAY_OF_MONTH, 5);
nextDate.setField(Calendar.MONTH_OF_YEAR, date.getField(Calendar.MONTH_OF_YEAR)+1); // issues in december
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜