Michal Žídek
Michal Žídek

Reputation: 494

Android Alarm Manager doesn't work

So I have a BroadcastReceiver which looks like this:

public class UpdateReceiver extends BroadcastReceiver {

    public UpdateReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, 10);
        System.out.println("Broadcast received");       

        PendingIntent operation = PendingIntent.getService(context, 0,new Intent("REFRESH_THAT"), PendingIntent.FLAG_UPDATE_CURRENT);       
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, System.currentTimeMillis(), cal.getTimeInMillis(), operation);
    }
}

This is how I call the BroadcastReceiver

 Intent in = new Intent ("REFRESH_BROADCAST");       
 sendBroadcast(in);

And this is my intent-filter in Android Manifest file

<service android:name = ".services.RefreshService">     
    <intent-filter>
        <action android:name="REFRESH_THAT"/>
    </intent-filter>
</service>

<receiver android:name=".services.UpdateReceiver">        
    <intent-filter>
        <action android:name="REFRESH_BROADCAST"/>        
    </intent-filter>    
</receiver>

BroadcastReceiver received a brodcast without any problem, but AlarmManager seems to do nothing. If I call operation.send() it works without a problem, so I presume there is something wrong with AlarmManager.

Upvotes: 2

Views: 2360

Answers (2)

Michal Ž&#237;dek
Michal Ž&#237;dek

Reputation: 494

Alright so finally I found a solution a it was my fault.

I have set int type to AlarmManager.ELAPSED_REALTIME and long triggerAtMillis to System.currentTimeMillis() property of alarmManager.setInexactRepeating(.....) which is wrong, it only can be paired with AlarmManager.RTC / RTC_WAKEUP.

So the functional code is this:

PendingIntent operation = PendingIntent.getService(context, 0,new Intent("REFRESH_THAT"), 0);       
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() , 30000, operation);

I should really read API documentation more carefully. If you hover AlarmManager.ELAPSED_REALTIME , it will told you what kind of time trigger you have to use. Hope this my stupid mistake will help someone in future.

Upvotes: 3

iTurki
iTurki

Reputation: 16398

As per the setInexactRepeating doc, The third parameter is:

intervalMillis: interval in milliseconds between subsequent repeats of the alarm

Then you should put 10000 ms (which is 10 sec) instead of cal.getTimeInMillis()

alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, cal.currentTimeMillis(), 10000, operation);

Which means that, youer alarm will go off after 10 sec of the intent firing, then will repeat each 10 sec. And of course, since you are using this method, This is inexact timing.

Upvotes: 0

Related Questions