John Smith
John Smith

Reputation: 752

AlarmManager fires twice

I am trying to use Androids AlarmManager. The problem is, it fires twice right after one another.

public class ScheduleSomething {

    public static void schedule(Pojo pojo) throws JsonProcessingException {

        final ObjectMapper mapper = new ObjectMapper();
        final String pojoAsJson = mapper.writeValueAsString(pojo);

        final Intent alarmIntent = new Intent(MyApplication.getAppContext(),
                PojoAlarmReceiver.class);
        alarmIntent.putExtra(POJO_AS_JSON, pojoAsJson);

        final PendingIntent pi = PendingIntent.getBroadcast(MyApplication.getAppContext(), 0,
                alarmIntent, PendingIntent.FLAG_ONE_SHOT);

        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + pojo.getScheduledTime() + 10L, pi);
    }
}

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        // do stuff. onReceive() is being called twice, every time.
    }

}

Ofcourse I have checked if I call the schedule(Pojo pojo) method twice but sadly, that's not it. Right now, I work around this but sooner or later, I'd like to resolve this in a clean way. Best regards and hopefully someone knows what's going wrong.

Upvotes: 3

Views: 2906

Answers (4)

Daniel Wilson
Daniel Wilson

Reputation: 19824

My problem, and this is going to sound stupid but might help someone, was that the Android Studio debugger for some reason "hits" the break point in my alarm's broadcast receiver twice, but it actually only executes the code once when no debugger is attached.

You can tell by logging something instead of using a break point.

Upvotes: 0

Jemshit
Jemshit

Reputation: 10038

After i had specified intent-filter on manifest, for my alarm broadcast receiver, i didn't have problem. It does trigger at some wrong time (few seconds) because of alarm.setInexactRepeating() but not twice in a single second as before.

<receiver
        android:name=".AlarmReceiver"
        android:process=":remote" >
        <intent-filter>
            <action android:name="com.mine.alarm"/>
        </intent-filter>
</receiver>

Also check this

Upvotes: 1

Kartik
Kartik

Reputation: 78

One Possibility could be , you might have registered the broadcast receiver twice one in the manifest file and other in the service if you use any. As it is registered twice you have two instance of the receiver. Hope this helps.

Upvotes: 1

John Smith
John Smith

Reputation: 752

Ok, I am sure there are more elegant ways to solve my problem but I have found a working sollution. The problem was that right after the BroadcastReceiver intercepted the Alarm, I made a call which then again was intercepted by this BC (eventhough this particular BC doesn't filter for NEW_OUTGOING_CALL nor PHONE_STATE...). So what I did is adding another String extra and check for it inside AlarmReceiver:

public class ScheduleSomething {

    public static final String  SOURCE = "source";

    public static void schedule(Pojo pojo) throws JsonProcessingException {

        final ObjectMapper mapper = new ObjectMapper();
        final String pojoAsJson = mapper.writeValueAsString(pojo);

        final Intent alarmIntent = new Intent(MyApplication.getAppContext(),
                PojoAlarmReceiver.class);
        alarmIntent.putExtra(POJO_AS_JSON, pojoAsJson);
        alarmIntent.putExtra(SOURCE, "com.mypackage.alarm");

        final PendingIntent pi = PendingIntent.getBroadcast(MyApplication.getAppContext(), 0,
                alarmIntent, PendingIntent.FLAG_ONE_SHOT);

        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + pojo.getScheduledTime() + 10L, pi);
    }
}

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        final String source = intent.getStringExtra(RegistrationCallLogic.SOURCE);

        if ((source == null) || !source.equals("com.mypackage.alarm")) {
                return;
        }
        // do stuff. onReceive() is being called twice, every time.
    }

}

Upvotes: 1

Related Questions