Search code examples
androidalarmmanagerandroid-alarmsrepeatingalarm

Multiple alarm crash


I am currently developing a reminder app and i have so far set up 2 alarms: One that goes off every day (AlarmX) and if it is the correct date, sets up another alarm(Alarm) which wakes the screen.

This code made sense in my head, but it doesn't perform what i want it to, and the logcat displays error that i don't understand how he caught.

Here is the code,maybe you see something i don't.

AlarmX - class that extends BroadcastReceiver

    public void SetAlarm(Context context)
{
    AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmX.class);
    intent.putExtra(ONE_TIME, Boolean.FALSE);
    intent.setAction("com.todo.list.brodcast.ALARMX");
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
    //After after 30 seconds
    Calendar c = Calendar.getInstance();
    Calendar k = Calendar.getInstance();
    k.set(Calendar.PM, 12);
    if(c.getTimeInMillis()-k.getTimeInMillis()>0) k.setTimeInMillis(k.getTimeInMillis()+24*60*60*1000);
    am.setRepeating(AlarmManager.RTC_WAKEUP, k.getTimeInMillis() , 24*60*60*1000 , pi); }

This method crashes the application and the onReceive() method of this class never gets called. I call it from Fragment like this:
AlarmX alarm = new AlarmX(); alarm.SetAlarm(getActivity().getApplicationContext()); And there is the Alarm class - same as AlarmX, but according to log cat gets the Error, even though it isn't called.

public void onReceive(Context context, Intent intent) {
    /*acquire power service manager*/
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    @SuppressWarnings("deprecation")
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP,"TAG");
    wl.acquire();

    //TODO creaate Notification object
    Bundle b = intent.getBundleExtra("Task");
    UUID id = UUID.fromString(b.getString("UUID"));
    Notification n = new Notification(context, id);
    n.setDescription(b.getString("Description"));
    n.setTitle(b.getString("Topic"));
    n.notifyUser();

    wl.release();
}

Now to my understanding when in AlarmX the am.setRepeating() command triggers onReceive() method in Alarm even though i didn't call alarm and the onReceive() creates a null pointer exception because i didn't pass a bundle to it. Here is the logcat output...

12-18 22:36:22.857: E/AndroidRuntime(30124): FATAL EXCEPTION: main
12-18 22:36:22.857: E/AndroidRuntime(30124): Process: com.todo.list:remote, PID: 30124
12-18 22:36:22.857: E/AndroidRuntime(30124): java.lang.RuntimeException: Unable to start receiver com.todo.list.brodcast.Alarm: java.lang.NullPointerException
12-18 22:36:22.857: E/AndroidRuntime(30124):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2856)
12-18 22:36:22.857: E/AndroidRuntime(30124):    at android.app.ActivityThread.access$1700(ActivityThread.java:156)
12-18 22:36:22.857: E/AndroidRuntime(30124):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440)
12-18 22:36:22.857: E/AndroidRuntime(30124):    at android.os.Handler.dispatchMessage(Handler.java:102)
12-18 22:36:22.857: E/AndroidRuntime(30124):    at android.os.Looper.loop(Looper.java:157)
12-18 22:36:22.857: E/AndroidRuntime(30124):    at android.app.ActivityThread.main(ActivityThread.java:5872)
12-18 22:36:22.857: E/AndroidRuntime(30124):    at java.lang.reflect.Method.invokeNative(Native Method)
12-18 22:36:22.857: E/AndroidRuntime(30124):    at java.lang.reflect.Method.invoke(Method.java:515)
12-18 22:36:22.857: E/AndroidRuntime(30124):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
12-18 22:36:22.857: E/AndroidRuntime(30124):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
12-18 22:36:22.857: E/AndroidRuntime(30124):    at dalvik.system.NativeStart.main(Native Method)
12-18 22:36:22.857: E/AndroidRuntime(30124): Caused by: java.lang.NullPointerException
12-18 22:36:22.857: E/AndroidRuntime(30124):    at com.todo.list.brodcast.Alarm.onReceive(Alarm.java:29)
12-18 22:36:22.857: E/AndroidRuntime(30124):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2845)
12-18 22:36:22.857: E/AndroidRuntime(30124):    ... 10 more

Thanks!

EDIT - Manifest.xml Here is how i declared the receivers in the Manifest file:

<receiver
        android:name="com.todo.list.brodcast.Alarm"
        android:exported="false"
        android:process=":remote" >
        <intent-filter>
            <action android:name="com.todo.list.brodcast.ALARM" />
        </intent-filter>
    </receiver>
      <receiver
        android:name="com.todo.list.brodcast.AlarmX"
        android:exported="false"
        android:process=":Asd" >
        <intent-filter>
            <action android:name="com.todo.list.brodcast.ALARMX" />
        </intent-filter>
    </receiver>

Solution

  • Why not just implement one Alarm that can go off and instead of calling the other, just replace the functionality?
    I think that will fix your problem, and simplify the code.