Android AlarmManager设置重复不工作
我想创建一个每小时发出一次通知的警报 我的代码在我的仿真器android 8.0上可以工作,但在我的设备上不行,我有oneplus 3T android 8.0。我确实允许在手机中使用我的应用程序来显示通知 我在onCreate中执行此操作Android AlarmManager设置重复不工作,android,android-alarms,Android,Android Alarms,我想创建一个每小时发出一次通知的警报 我的代码在我的仿真器android 8.0上可以工作,但在我的设备上不行,我有oneplus 3T android 8.0。我确实允许在手机中使用我的应用程序来显示通知 我在onCreate中执行此操作 calendar = Calendar.getInstance(); intent = new Intent(this, MyReceiver.class); pendingIntent = PendingIntent.getBroadcast(this,
calendar = Calendar.getInstance();
intent = new Intent(this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am = (AlarmManager)this.getSystemService(ALARM_SERVICE);
am.cancel(pendingIntent);
这就是我在onDestroy做的
protected void onDestroy()
{
// telling the client thread to close
c.setCommand(6);
// setting an notification alarm every hour
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 60, pendingIntent);
super.onDestroy();
}
更新:
我发现AlarmManager在应用程序关闭时没有触发我的通知,因此我是否需要使用服务来设置AlarmManager?基于以下内容:
标准AlarmManager
报警(包括setExact()和setWindow())
延迟到下一个维护窗口
- 如果需要设置打盹时触发的警报,请使用setAndAllowHileId()或setExactAndAllowHileId()
- 使用setAlarmClock()设置的警报将继续正常触发-系统在警报触发前不久会退出休眠状态
如果您的用例不需要在特定的时间触发,请考虑使用<代码> WorkMase< /C> >
< P>在Android 8中显示通知,您需要使用NoTeChanChanChank,而不定义通知通道,您不能显示来自任何源的通知(在使用ALARMMARS管理器的情况下) 对于Android版本>8.0(Oreo)我确实使用通知频道。我写道,通知代码在我的emuletor上运行,但在我的手机上不工作,它们都运行在8.0上。然后使用这个,public void setExactAndAllowHileId(int-type,long triggerAtMillis,pendingEvent操作)//而不是setRepeating。在空闲模式下唤醒设备。但我的手机没有空闲。我在手机屏幕打开时测试闹钟,但我的手机没有空闲。我在手机屏幕打开时测试闹钟。
// Setup a NotificationChannel
public static final String ALARM_CHANNEL_ID = "alarm";
public static final String ALARM_CHANNEL_NAME = "Alarm Channel";
NotificationChannel alarmNotificationChannel = new NotificationChannel(ALARM_CHANNEL_ID, ALARM_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
String description = "Media playback controls";
playNotificationChannel.setDescription(description);
playNotificationChannel.setShowBadge(false);
playNotificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
getNotificationManager().createNotificationChannel(playNotificationChannel);
notification = new Notification.Builder(context)
.SetChannelId(ALARM_CHANNEL_ID)
.SetContentTitle(title)
.SetContentText(message)
.SetAutoCancel(true)
.SetSmallIcon(Resource.Drawable.icon)
.SetContentIntent(pending)
.Build();
//use the notification as you are already using it.
}