gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:23:55 28 4
gpt4 key购买 nike

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

因此,当第一个警报被调用时,它会打开 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);
}
}

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

我如何设置闹钟:

    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")) {

现在我该如何继续以正确的方式在 broadcastreceiver 中设置警报?谢谢!

最佳答案

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

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,但是您使用变量 将所有“extras”放入了 Intent Intent 。然后将 intentAlarm 作为参数传递给 PendingIntent.getBroadcast()。当然 intentAlarm 中没有任何“额外”。

尝试将所有“额外”放在 intentAlarm 中。

关于android - 如何在广播接收器中设置闹钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45761029/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com