user2495899
user2495899

Reputation:

AlarmManager setRepeating()

My program is designed to create a repeating alarm that triggers a broadcastreceiver in turn making a notification. The alarm is repeated using a user-entered interval.

For example, if i want to set the alarm to run every 10 seconds, how would I do that?

 am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),  10000, calpendingintent);

Is this right? and my broadcast receiver isn't being called either for some reason.

public static void createAlarms(Context mcontext) {
    cal = Calendar.getInstance();
    cal.add(Calendar.HOUR, alarmintervalint);
    calintent = new Intent(mcontext, AlarmBroadcastReceiver.class);
    calpendingintent = PendingIntent.getBroadcast(mcontext.getApplicationContext(), 12345, calintent, 0);
    am = (AlarmManager)mcontext.getSystemService(Activity.ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),  10000, calpendingintent);
}

My broadcastreceiver class is not being called and Im not sure the "setRepeating()" method Im using is set correctly..

Please help!

Upvotes: 8

Views: 35438

Answers (4)

Praveen Sharma
Praveen Sharma

Reputation: 4348

you can use this. Hope it solves your problem

am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
    SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY,
    AlarmManager.INTERVAL_DAY, pi);

Upvotes: -1

Chanaka Fernando
Chanaka Fernando

Reputation: 2335

Use setInexactRepeating() instead of setRepeating(). When you use setInexactRepeating(), Android synchronizes repeating alarms from multiple apps and fires them at the same time.

This reduces the total number of times the system must wake the device, thus reducing drain on the battery. As of Android 4.4 (API Level 19), all repeating alarms are inexact.

Note that while setInexactRepeating() is an improvement over setRepeating(), it can still overwhelm a server if every instance of an app hits the server around the same time. Therefore, for network requests, add some randomness to your alarms

Upvotes: 3

Saeed Parand
Saeed Parand

Reputation: 33

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, 30);

    long time = cal.getTimeInMillis();

    Intent i = new Intent(G.context, BootCompleteReceiver.class);

    PendingIntent pi = PendingIntent.getBroadcast(G.context, 0, i, 0);

    G.alarmManager.set(AlarmManager.RTC_WAKEUP, time, pi);

Upvotes: -1

Dixit Patel
Dixit Patel

Reputation: 3192

use this code

  AlarmManager alarmMgr;
  PendingIntent pendingIntent;

    public void startAlarmManager()
        {
             Intent dialogIntent = new Intent(getBaseContext(), AlarmBroadcastReceiver.class);

                  alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
                  pendingIntent = PendingIntent.getBroadcast(this, 0, dialogIntent,PendingIntent.FLAG_CANCEL_CURRENT);

                  alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(), 10000, pendingIntent);

              }
        }

wheather you want to stop alarm

public void stopAlarmManager()
{          
    if(alarmMgr != null)
       alarmMgr.cancel(pendingIntent);
}

Be Remembered dont forget to register Receiver in manifest file

  <receiver android:name=".AlarmBroadcastReceiver" >
        </receiver>

Upvotes: 7

Related Questions