2

I have been developing an Android application and I need to execute 1 task every hour. I uses the following code for it:

private static final long ALARM_PERIOD = 1000L;

public static void initAlarmManager(Context context) {

    Editor editor=PreferenceManager.getDefaultSharedPreferences(context).edit();
    editor.putBoolean(context.getString(R.string.terminate_key), true).commit();

    AlarmManager manager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
    Intent i = new Intent(context, AlarmEventReceiver.class);
    PendingIntent receiver = PendingIntent.getBroadcast(context, 0, i, 0);
    manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime(), ALARM_PERIOD, receiver);
}

It works for me, but my client tells me that the task works only 1 time and won't work 1 hour. Where have I made a mistake? Please, tell me. Thank you.

user1166635
  • 2,741
  • 6
  • 22
  • 32

4 Answers4

4

According to your code, ALARM_PERIOD is 1000L, as repeating interval. So I doubt the alarm will set of in every 1000 milliseconds.

if you are setting repeating interval for every hour, it should be 3600000L. And take note that if the phone is restarted, your alarm manager will no longer work unless you start again.

Here is the my Code:

private void setAlarmManager() {
    Intent intent = new Intent(this, AlarmReceiver.class);
    PendingIntent sender = PendingIntent.getBroadcast(this, 2, intent, 0);
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    long l = new Date().getTime();
    if (l < new Date().getTime()) {
        l += 86400000; // start at next 24 hour
    }
    am.setRepeating(AlarmManager.RTC_WAKEUP, l, 86400000, sender); // 86400000
}
NyanLH
  • 480
  • 1
  • 6
  • 17
  • Sorry, I forgot to fix it. I uses this value in order to test on my device, but for client I sent the application with needed value. It isn't a cause of the wrong work. – user1166635 May 17 '12 at 05:57
  • http://stackoverflow.com/questions/5938213/android-alarmmanager-rtc-wakeup-vs-elapsed-realtime-wakeup – NyanLH May 17 '12 at 06:06
  • So, please, give me an example how I should set values for manager.setRepeating() instead my code. Thank you. – user1166635 May 17 '12 at 06:11
2

Have you added receiver tag in application tag in manifest.xml
<receiver android:name=".AlarmReceiver" android:process=":remote"/>

Dhrupal
  • 1,863
  • 1
  • 23
  • 38
1

Instead of Alram-Manager I recommended you to use Android-TimerTask

The TimerTask class represents a task to run at a specified time. The task may be run once or repeatedly. Its perfect suits for your requirements.

user370305
  • 108,599
  • 23
  • 164
  • 151
  • I think it isn't good idea, because my application doesn't do anymore then executes task every 1 hour, i.e. the app doesn't have Activities. When an user executes the application it will only execute schedule and the screen will be closed. If the Timer Task will work in this case? – user1166635 May 17 '12 at 05:52
  • TimerTask will run even your screen was close. Or you are executing it in Service. – user370305 May 17 '12 at 05:54
  • Thank you, but why you recommend me using TimerTask instead AlarmManager? – user1166635 May 17 '12 at 06:02
0

Try by modifying your code by changing your setRepeating() method like this

manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime(), SystemClock.elapsedRealtime()+(60*60*1000), receiver);

OR

Test this it is repeating for every minute

manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,Calendar.getInstance().getTimeInMillis(), Calendar.getInstance().getTimeInMillis()+(1*60*1000), receiver);
WuerfelDev
  • 140
  • 1
  • 13
Raghu Mudem
  • 6,793
  • 13
  • 48
  • 69