Search code examples
androidbroadcastreceiver

Broadcast receiver throws error after reboot when application was not opened before


I have a broadcast receiver, launching after reboot to set several alarms with the help of the alarm manager. The broadcast and the receiving after the reboot works perfectly, however the alarm being set in this broadcast throws an error when being fired.

E/AndroidRuntime(4330):     at mobileTechnology.geoCalendar.SetAlarm.checkReminder(SetAlarm.java:82)
E/AndroidRuntime(4330):     at mobileTechnology.geoCalendar.SetAlarm.access$1(SetAlarm.java:80)
E/AndroidRuntime(4330):     at mobileTechnology.geoCalendar.SetAlarm$1.gotLocation(SetAlarm.java:73)
E/AndroidRuntime(4330):     at mobileTechnology.geoCalendar.CurrentLocation$2.onLocationChanged(CurrentLocation.java:70)

When I open the application after the reboot before the other alarms are fired, there will be no error. Do you have any idea, why the app is behaving that way? In the broadcast receiver I'm accessing a normal java class (no activity) to determine the current position by a callback. Is there a problem accessing this class when the application was not opened before?

EDIT: I guess I configured my manifest file the wrong way, as there is missing an Intent filter. Can anyone tell me what's the right way to do it?

<receiver android:name="SetAlarm"></receiver>

EDIT2:

thank you, so here is some more code of my project:

During runtime a set up several alarms for events by using the alarm manager. The alarm is created by calling

new SetAlarm (Context context, Bundle bundle, Long time_how_far_in_advance_alarm_should_be_fired)

The bundle contains detailed information about the event (name, time ...) and the SetAlarm constuctor looks like the following:

AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, SetAlarm.class);
intent.putExtra("data", bundle);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, bundle.getInt("id"), intent,PendingIntent.FLAG_UPDATE_CURRENT);

Calendar cal = Calendar.getInstance();
*<set time for reminder>*
alarmMgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                  pendingIntent);

The receiver is in the same class as the broadcast and defined by

<receiver android:name="SetAlarm"></receiver>

Until this point everything works perfect as alarms are being fired when they should be and a notification will be displayed.

To reload the alarms after a reboot, there is another class ReloadAlarms, loading all Events out of a database and setting up the alarms again.

public void onReceive(Context context, Intent intent)

<receiver android:name="ReloadAlarms" >
        <intent-filter >
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
</receiver>

The alarms are set up again by calling SetAlarm and passing the context, just received from the first on receive method. SetAlarm(Context context, Bundle bundle, long reminder)

As soon as the onReceive method in SetAlarm is called, it validates if the alarm has to be set to another time. If yes I call new SetAlarm(_context, newBundle, newTime); _context is again the context I just received from the onReceive method.

The problem is that the onReceive method is called continously. (at least in case a reschedule of the event is necessary, even without reboot)

How does the onReceive method recognize, when an alarm was fired. Is it possible that this happens because I'm passing the context received by the onReceive method to new SetAlarm(...) for the reschedule?

END Edit2

Thank you for your help!


Solution

  • ok, I finally found the error...the problem was because I didn't initialize my Setting singleton after reboot. I implemented an initializeSetting function, that is called in my main activity. I thought it's because of the implementation of the broadcast receiver all time....

    Now I initialize the settings (if not already happened) also in the broadcast receiver and it works.

    Stupid mistake that cost me hours -.-