gpt4 book ai didi

java - 警报通知未出现

转载 作者:行者123 更新时间:2023-11-30 00:25:02 24 4
gpt4 key购买 nike

我在获取每日通知时遇到了一些问题。

我的应用程序获取用户希望提醒某事的时间、小时和分钟。

然后我使用 AlarmManager 设置一个重复的闹钟,同时使用闹钟接收器创建通知。

我已经尝试了几个小时,但无法弄清楚我做错了什么。

我已经查看了很多其他 SO 问题,但没有一个对我有帮助。

我已将用户输入的小时和分钟存储到日期对象 get habitReminder 中。

我的createNotifications() 方法:

private void createNotifications() {
Log.i("Reminder at",""+habitReminder.getHours()+":"+habitReminder.getMinutes());
//Create the alarms/notifications for the user
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
alarmIntent.putExtra("name", habitName);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);

Log.i("createNotifications", "Alarm manager is created.");

//Set the timing of the reminder
Calendar calendar = Calendar.getInstance();
Calendar now = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, habitReminder.getHours());
calendar.set(Calendar.MINUTE, habitReminder.getMinutes());
calendar.set(Calendar.SECOND,0);

//Check to make sure time is after the current date.
if(calendar.before(now)){
calendar.add(Calendar.DATE, 1);
}

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

Log.i("createNotifications", "Alarm has been set for " +habitReminder.getHours()+":"+habitReminder.getMinutes() +" daily.");
}

我的报警接收器类:

public class AlarmReceiver extends BroadcastReceiver {

private static int id =0;

@Override
public void onReceive(Context context, Intent intent) {
String name = intent.getStringExtra("name");
String title = name + " Reminder!";
String message = "Your reminder to keep up your habit!";
long when = System.currentTimeMillis();
Intent in = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context,0,in,PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager nM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(context)
.setContentIntent(contentIntent)
.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setWhen(when);
Notification notification = builder.build();
nM.notify(id,notification);
id++;
}

还有我的 android list :

<receiver android:name="com.closedbracket.trackit.AlarmReceiver" android:enabled="true">
</receiver>

任何帮助将不胜感激。

最佳答案

如果您想要精确可靠的闹钟,请使用setAlarmClock。它会消耗更多电池电量,但您确定闹钟会在设置的时间准确响起。

更多信息可以引用Difference between setExact and setAlarmClock

关于java - 警报通知未出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45558266/

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