3

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.

John Smith
  • 752
  • 9
  • 35
  • 2
    Uninstall your app completely. Use **`adb shell dumpsys alarm`** to confirm that you no longer have any alarms scheduled. Install your app and run through your code to set up the alarms. Then use **`adb shell dumpsys alarm`** again. If you see multiple entries in there for your app, you are scheduling multiple alarms. If not, figure out what else in your app is triggering your `AlarmReceiver`. – CommonsWare Jun 26 '14 at 12:45
  • There's a whole bunch scheduled, but not a single Intentrecord mentioning my packae name, most of them refer to com.google.android.calendar and com.google.android.gms and other goole services related stuff. And while doing so I realize that I don't register an intent filter that filters for my own package identifier. I suppose that's the root cause, right? – John Smith Jun 26 '14 at 14:06
  • Addendum to above comment: Thats exactly what's going wrong. The BC triggers ofcourse everytime anything worth intercepting is coming in. I will update as soon as I have straightned my code. – John Smith Jun 26 '14 at 14:14
  • "And while doing so I realize that I don't register an intent filter that filters for my own package identifier. I suppose that's the root cause, right?" -- no. You are using an explicit `Intent`, and so you don't need an ``. If you *do* have an ``, though, your additional `onReceive()` calls could be from other broadcasts matching that filter. – CommonsWare Jun 26 '14 at 14:21
  • Not sure what to make of this but I solved it by adding another extra to the Intent I declare inside the schedule() method, check inside the BC for that particular extra and drop out if it's not there – John Smith Jun 26 '14 at 14:25
  • Something else is also creating an `Intent` pointing to your receiver. You should try to track down what that is. – CommonsWare Jun 26 '14 at 14:27

4 Answers4

1

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.
    }

}
John Smith
  • 752
  • 9
  • 35
1

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.

Kartik
  • 78
  • 1
  • 7
1

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

Community
  • 1
  • 1
Jemshit
  • 9,501
  • 5
  • 69
  • 106
0

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.

Daniel Wilson
  • 18,838
  • 12
  • 85
  • 135