java - 警报在到期时正确收到,但在之后打开应用程序时第二次不正确
问题描述
我可以通过使用日历类 java(例如上午 9:15)在未来的特定时间获得通知。只要时间是上午 9:15,但当我在上午 9:15 之后(例如上午 9:20)打开应用程序时,警报管理器就会将广播发送到广播接收器。我收到同样的通知。任何人都可以帮助我解决这个问题。
我的清单
<receiver android:name=".others.NotificationReceiver"/>
通知接收器.java
public class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
createNotification(context);
}
private void createNotification(Context context) {
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationChannel linkChannel = new NotificationChannel(link_channel_id, "Link Channel", NotificationManager.IMPORTANCE_HIGH);
linkChannel.setDescription("This channel is used to show Zoom Links");
notificationManager.createNotificationChannel(linkChannel);
}
Notification notification = new NotificationCompat.Builder(context, link_channel_id)
.setSmallIcon(R.mipmap.ic_launcher_foreground)
.setContentTitle("Test Notification")
.setContentText("This is a test notification")
.setPriority(NotificationCompat.PRIORITY_MAX)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.build();
notificationManager.notify(1, notification);
}
}
我的警报管理器
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 10);
calendar.set(Calendar.SECOND, 0);
Intent myIntent1 = new Intent(this, NotificationReceiver.class);
myIntent1.setAction(ACTION_ONE);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this, 1, myIntent1, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent1);
解决方案
推荐阅读
- matlab - matlab精度处理大数
- javascript - 使用 angular6 中的预选数据选中复选框
- docker - 在 Dockerfile 中运行 /usr/sbin/init 和 salt-master
- java - 拍摄整个活动的照片有时会导致 App 崩溃
- sql - 如何找到包括当前日期在内的有用日期(Oracle)
- java - 如何在appium Java IOS中上下左右滑动
- python-3.x - python3多处理队列死锁,同时使用线程和进程
- java - 我收到 IOException 错误,try-catch 并没有救我
- typescript - 获取错误 TS1241:作为表达式调用时无法解析方法装饰器的签名
- implementation - VC2015 中的 FMI 实现