Michael
Michael

Reputation: 71

Alarmmanager fires first time but doesn't repeat

I want an Alarmmanager to fire first time after 2 Seconds and then every 10 Sec.

It doesn't fire the first time exactly 2 seconds later. Something between 5 and 10 sec later. And it doesn't repeat at all.

Here is my Code:

Alarmmanager:

Intent intent = new Intent(this, BackgroundService.class);
final PendingIntent pendingintent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);

final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis() + 2 * 1000, 10 * 1000, pendingintent);

Manifest:

<receiver android:process=":remote" android:name=".BackgroundService"/>

BackgroundService.java:

public class BackgroundService extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("BackgroundService", "BackgroundService onReceive");
    }
}

Upvotes: 2

Views: 1057

Answers (3)

Michael
Michael

Reputation: 71

Got it:

The IDE says: The 3rd value will be forced up to 60000 (1 Min) to save battery. But I never waited so long, so it looked like it doesn't even repeates.

Snap is here

Upvotes: 3

Android Android
Android Android

Reputation: 724

this heslp me

if (android.os.Build.VERSION.SDK_INT >= 19) {
                alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
            } else {
                alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
            }

Upvotes: 0

Surya
Surya

Reputation: 638

am.setRepeating(AlarmManager.RTC_WAKEUP, **cal_alarm.getTimeInMillis()**, 1000*60*5 , pendingIntent); 

I think you are using System.currentTimeMillis() because of which you are having that issue. Try to get the time from you code for which you have set the Alarm. The third parameter in the above code is the repeating time. You can set it to 10 sec.

Upvotes: 1

Related Questions