如何在广播接收器中设置闹钟

How to set an alarm inside a broadcastreceiver

在我的应用程序中,用户可以选择让他的活动重复,每次重复活动都会有一个通知(警报)来提醒用户他的活动即将开始。

因此,当第一个警报被调用时,它会打开 BroadcastReceiver,创建通知并应为下一次重复设置警报:

    @Override
public void onReceive(Context context, Intent intent) {
    // When onReceive gets called the event notification is triggered
    DatabaseHelper myDatabaseHelper = new DatabaseHelper(context);

        // Create notification
        int id = intent.getIntExtra("id", 0);
        String name = intent.getStringExtra("name");
        long notificationTime = intent.getLongExtra("notificationTime", 0);
        String recurrenceType = intent.getStringExtra("recurrenceType");
        String message = intent.getStringExtra("message");

        if (recurrenceType.equals("Every week")) {
            // repeatInterval = unix timestamp so * 1000 to get in Millis for notificationTime
            long repeatInterval = 604800 * 1000;
            notificationTime += repeatInterval;
            myDatabaseHelper.updateNotificationTime(notificationTime);
            setAlarm(context, intent, id, name, notificationTime, recurrenceType, message);
        } else {
            myDatabaseHelper.updateNotificationTime(0);
        }

        createNotification(context, message, name, id);
    }
}

现在这对于事件的第一个警报来说工作正常。通知已创建。现在,当调用第二个警报时,意图为空,因此在第一个警报之后,所有其他意图都为空,并且创建了一个空通知,然后应用程序崩溃并出现 NullPointerException。注意:我让我的应用程序和我的设备保持打开状态。 setAlarm(...) 方法在 BroadcastReceiver class.

内部

我如何设置闹钟:

    public void setAlarm(Context context, Intent intent, int id, String name, long notificationTime, String recurrenceType, String message){
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intentAlarm = new Intent(context, AlarmReceiver.class);
    intent.putExtra("id", id);
    intent.putExtra("name", name);
    intent.putExtra("notificationTime", notificationTime);
    intent.putExtra("recurrenceType", recurrenceType);
    intent.putExtra("message", message);
    // A PendingIntent specifies an action to take in the future [context, request code, intent, flag]
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
    // RTC_WAKEUP will wake up the device when it goes off
    alarmManager.set(AlarmManager.RTC_WAKEUP, notificationTime, pendingIntent);
}

堆栈跟踪:

08-25 12:12:35.917 19323-19323/com.madgusto.when2game E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: com.madgusto.when2game, PID: 19323
                                                                    java.lang.RuntimeException: Unable to start receiver com.madgusto.when2game.alarm.AlarmReceiver: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
                                                                        at android.app.ActivityThread.handleReceiver(ActivityThread.java:3018)
                                                                        at android.app.ActivityThread.-wrap18(ActivityThread.java)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1544)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:154)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
                                                                     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
                                                                        at com.madgusto.when2game.alarm.AlarmReceiver.onReceive(AlarmReceiver.java:48)
                                                                        at android.app.ActivityThread.handleReceiver(ActivityThread.java:3011)
                                                                        at android.app.ActivityThread.-wrap18(ActivityThread.java) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1544) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                        at android.os.Looper.loop(Looper.java:154) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

因此空指针位于:

if (recurrenceType.equals("Every week")) {

现在我该如何继续以正确的方式在广播接收器中设置闹钟?谢谢!

很清楚发生了什么。看这段代码:

Intent intentAlarm = new Intent(context, AlarmReceiver.class);
intent.putExtra("id", id);
intent.putExtra("name", name);
intent.putExtra("notificationTime", notificationTime);
intent.putExtra("recurrenceType", recurrenceType);
intent.putExtra("message", message);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);

您在变量 intentAlarm 中创建了一个 Intent,但是您使用变量 intent 将所有 "extras" 放入了 Intent。然后将 intentAlarm 作为参数传递给 PendingIntent.getBroadcast()。当然intentAlarm里面没有任何"extras"。

尝试将所有 "extras" 放入 intentAlarm