Jonas Jonas - 2 months ago 5
Android Question

Android alarm manager does not wait

I try to start a simple method every 20 seconds, my idea was to start an alarm in this method again. To create this class again, in which the method is executed and is starting another alarm... and so on
The method itself should create a notification.

public class CreateNotification extends BroadcastReceiver{
public void onReceive(Context context, Intent intent) {


NotificationCompat.Builder mNoteBuilder =
new NotificationCompat.Builder(context)

//get an instance of the notificationManager service
NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//build the notification


private void createNewAlarm(Context context){
Intent alarmIntent = new Intent(context, CreateNotification.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
manager.set(AlarmManager.RTC_WAKEUP, 20000, pendingIntent);

this is started in my main activity:

Intent alarmIntent = new Intent(this, CreateNotification.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
manager.set(AlarmManager.RTC_WAKEUP, 4000, pendingIntent);

Now my problem is, that I don't get the expected result, a new notification every 20 seconds, but it creates all the time notification, as fast as the processor allows. There is no break in between and the alarmmanager seems as it does not schedule anything, but just creates the class instantly.

Thanks a lot for your help!

manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+20000, pendingIntent);

your solution: 20000 = 1970. january 1. 0:00:20 so you need to add your millisec to current time. (Another solution to get current time.)

Calendar calendar = Calendar.getInstance();