Bishal
Bishal

Reputation: 29

Repeating Alarm in Android Monthly

I am Having Some issues while setting a Repeating Alarm which shall trigger on a particular Date(Entered by user) for every month or every two month. So far, I am using service for notification, BroadcastReceiver along with pending Intent. What i am unable to understand is:

alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY * 30*month, pendingIntent);

How can we set the function here and how is that going to affect the battery life and is there any other things( such as storing date in database and only call it when something triggers?) etc.. 1.Notification Service Extending service

public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        // Getting Notification Service
        mManager = (NotificationManager) this.getApplicationContext()
                .getSystemService(
                        this.getApplicationContext().NOTIFICATION_SERVICE);
        /*
         * When the user taps the notification we have to show the Home Screen
         * of our App, this job can be done with the help of the following
         * Intent.
         */
        Intent intent1 = new Intent(this.getApplicationContext(), com.expandablelistItems.demo.adapter.DynamicActivity.class);

        Notification notification = new Notification(R.drawable.ic_launcher,
                "Payment of your demoReminder", System.currentTimeMillis());

        intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
                | Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
                this.getApplicationContext(), 0, intent1,
                PendingIntent.FLAG_UPDATE_CURRENT);

        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        notification.setLatestEventInfo(this.getApplicationContext(),
                "demo", "Payment of your demoReminder",
                pendingNotificationIntent);

        mManager.notify(0, notification);
    }

2. Method for repeatation

if  (current_Month == Calendar.FEBRUARY){//for feburary month)
            GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();    
            if(cal.isLeapYear(calendar.get(Calendar.YEAR))){//for leap year feburary month  
                alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 29*month, pendingIntent);
                Toast.makeText(getActivity(), "februry", Toast.LENGTH_SHORT).show();}
            else{ //for non leap year feburary month
                Toast.makeText(getActivity(), "feb", Toast.LENGTH_SHORT).show();
                alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 28*month, pendingIntent);
            }
        }

and this is the broadcast receiver

 @Override
    public void onReceive(Context context, Intent intent) {
    // When our Alaram time is triggered , this method will be excuted (onReceive)
    // We're invoking a service in this method which shows Notification to the User
     Intent myIntent = new Intent(context, NotificationService.class);
     context.startService(myIntent);
   }

Where notificationService is the first code extending service

Upvotes: 1

Views: 3136

Answers (3)

Meghana M
Meghana M

Reputation: 535

Basically,

  • You can to initialize pendigIntent to the specific Intent which will receive the Alarm and trigger intended action.
  • AlarmManager.INTERVAL_DAY * 30*month - specifies the number of months(1month/2months) * 30 * daily interval(i.e 24 hr)
  • You can create a class like AlarmReceiver which will be having onReceive() method in which you can perform your action.

Regarding Battery life and other ways:

One of the ways is using ScheduleThreadExecutor class but which runs number of threads and using AlarmManager we can improve performance.

Instead of creating instance of AlarmManager you can use Context.getSystemService(Context.ALARM_SERVICE) as this is the system service. Which will optimize memory usage and battery life.

One more thing you can use setInexactRepeating instead of setRepeating, those alarms are more power-efficient

For detailed info you can visit to http://developer.android.com/reference/android/app/AlarmManager.html

Upvotes: 2

Alex.F
Alex.F

Reputation: 6201

You should avoid using SetRepeating with an interval of 30 days for two reasons:

  1. It won't be repeating on the same day of every month (also might skip a month potentially).
  2. Since API 19 SetRepeating is inexact and will function like SetInexactRepeating which means potentially shifting the alarm as much as a whole interval duration (up to 30 days in your case).
    As mentioned in the docs:

    Your alarm's first trigger will not be before the requested time, but it might not occur for almost a full interval after that time

As long as you want the alarm to trigger on the exact day you should use set(int, long, PendingIntent) and calculate next alarm after every instance.

Upvotes: 4

Lucifer
Lucifer

Reputation: 29670

While an Alarm will be vanished if your device is turned off. I suggest you to use an alternet way for triggering an action after a long period(like one month). Try Reminder or Calendar Events.

Please check the Reminder's Example

Please check the Calendar's Event Example

Upvotes: 0

Related Questions