Search code examples
androidstartupalarmmanager

AlarmManager on Reboot


I'm trying to create an alarm that resets itself when the device restarts. Is this the correct way to do it (assuming the Android manifest is correct)? The code seems to be crashing my app.

public class AlarmReceiver extends BroadcastReceiver {
     Context cxt;

 @Override
 public void onReceive(Context context, Intent intent) {
     cxt = context;
    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
        Calendar updateTime = Calendar.getInstance();
        updateTime.setTimeZone(TimeZone.getDefault());
        updateTime.set(Calendar.HOUR_OF_DAY, 0);
        updateTime.set(Calendar.MINUTE, 0);

        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1234, 
            intent, PendingIntent.FLAG_CANCEL_CURRENT);
        am.setRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);
    }
}

Here is the Logcat:

01-28 00:35:00.263: E/AndroidRuntime(31269): FATAL EXCEPTION: main
01-28 00:35:00.263: E/AndroidRuntime(31269): java.lang.RuntimeException: Unable to start receiver com.neelsomani.meds.AlarmReceiver: java.lang.NullPointerException
01-28 00:35:00.263: E/AndroidRuntime(31269):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2265)
01-28 00:35:00.263: E/AndroidRuntime(31269):    at android.app.ActivityThread.access$1600(ActivityThread.java:139)
01-28 00:35:00.263: E/AndroidRuntime(31269):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
01-28 00:35:00.263: E/AndroidRuntime(31269):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-28 00:35:00.263: E/AndroidRuntime(31269):    at android.os.Looper.loop(Looper.java:137)
01-28 00:35:00.263: E/AndroidRuntime(31269):    at android.app.ActivityThread.main(ActivityThread.java:4918)
01-28 00:35:00.263: E/AndroidRuntime(31269):    at java.lang.reflect.Method.invokeNative(Native Method)
01-28 00:35:00.263: E/AndroidRuntime(31269):    at java.lang.reflect.Method.invoke(Method.java:511)
01-28 00:35:00.263: E/AndroidRuntime(31269):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
01-28 00:35:00.263: E/AndroidRuntime(31269):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
01-28 00:35:00.263: E/AndroidRuntime(31269):    at dalvik.system.NativeStart.main(Native Method)
01-28 00:35:00.263: E/AndroidRuntime(31269): Caused by: java.lang.NullPointerException
01-28 00:35:00.263: E/AndroidRuntime(31269):    at com.neelsomani.meds.AlarmReceiver.onReceive(AlarmReceiver.java:40)
01-28 00:35:00.263: E/AndroidRuntime(31269):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2258)

Solution

  • This code seems good, can you give us more detail from the crash please?

    Have you declared this broadcastReceiver in the AndroidManifest.xml ?

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