Android 8.0(Oreo) 中的 AlarmManager 和通知

编程入门 行业动态 更新时间:2024-10-24 06:34:58
本文介绍了Android 8.0(Oreo) 中的 AlarmManager 和通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的应用程序中,我有一个每周四重复的闹钟,我正在使用 AlarmManger,在所有以前版本的 android 中一切正常,但现在使用 android 8.0(oreo),闹钟没有触发,下面是我用来设置闹钟的课程.从我搜索的内容来看,我需要明确设置闹钟,但我不明白怎么办.

In my app i have a alarm that is repeting every thursday, i am using AlarmManger, everything is working fine in all previous versions of android, but now with android 8.0(oreo), the alarm Isn't firing, below is the classes that i use to set my alarm. From what i searched i need to set the alarm explicitly, but I dont understand how.

主要活动:

try { Intent alarmIntent = new Intent(this, typeof(AlarmReceiver)); PendingIntent pending = PendingIntent.GetBroadcast(this, 0, alarmIntent, PendingIntentFlags.UpdateCurrent); AlarmManager alarmManager = GetSystemService(AlarmService).JavaCast<AlarmManager>(); if (Settings.AccessAlarm == 0) { alarmManager.SetRepeating(AlarmType.RtcWakeup, BootReceiver.FirstReminder(), BootReceiver.reminderInterval, pending); PendingIntent pendingIntent = PendingIntent.GetBroadcast(this, 0, alarmIntent, 0); Settings.AccessAlarm = 1; } } catch (Exception e) { Settings.AccessAlarm = 0; }

Bootreceiver.cs:

Bootreceiver.cs:

[BroadcastReceiver] [IntentFilter(new[] { Intent.ActionBootCompleted })] public class BootReceiver : BroadcastReceiver { //the interval currently every one minute //to set it to dayly change the value to 24 * 60 * 60 * 1000 public static long reminderInterval = AlarmManager.IntervalDay * 7; //public static long reminderInterval = 3 * 1000; public static long FirstReminder() { System.Random rnd = new System.Random(); int minutenumber = rnd.Next(20, 40); Java.Util.Calendar calendar = Java.Util.Calendar.Instance; calendar.Set(Java.Util.CalendarField.DayOfWeek, Calendar.Thursday); calendar.Set(Java.Util.CalendarField.HourOfDay, 10); calendar.Set(Java.Util.CalendarField.Minute, minutenumber); return calendar.TimeInMillis; } public override void OnReceive(Context context, Intent intent) { try { Console.WriteLine("BootReceiver: OnReceive"); var alarmIntent = new Intent(context, typeof(AlarmReceiver)); var pending = PendingIntent.GetBroadcast(context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent); AlarmManager alarmManager = (AlarmManager)context.GetSystemService(Context.AlarmService); alarmManager.SetRepeating(AlarmType.RtcWakeup, FirstReminder(), reminderInterval, pending); PendingIntent pendingIntent = PendingIntent.GetBroadcast(context, 0, alarmIntent, 0); Settings.AccessAlarm = 1; } catch (Exception e) { Settings.AccessAlarm = 0; } } }

报警接收器:

[BroadcastReceiver] public class AlarmReceiver : BroadcastReceiver { private int z = 0; private int i; public override void OnReceive(Context context, Intent intent) { try { Settings.AlarmNotification = 1; if (System.DateTime.Now.DayOfWeek == DayOfWeek.Thursday) { Settings.AlarmCount =0; } var title = "Test"; var message = "Something"; Intent backIntent = new Intent(context, typeof(MainActivity)); backIntent.SetFlags(ActivityFlags.NewTask); var resultIntent = new Intent(context, typeof(MainActivity)); PendingIntent pending = PendingIntent.GetActivities(context, 0, new Intent[] { backIntent, resultIntent }, PendingIntentFlags.OneShot); var builder = new Notification.Builder(context) .SetContentTitle(title) .SetContentText(message) .SetAutoCancel(true) .SetSmallIcon(Resource.Drawable.icon) .SetDefaults(NotificationDefaults.All); builder.SetContentIntent(pending); var notification = builder.Build(); var manager = NotificationManager.FromContext(context); manager.Notify(1331, notification); } catch (Exception) { } } }

推荐答案

当您使用显式意图时,您的警报将正确触发.但在 Oreo/API26 中 .SetDefaults(NotificationDefaults.All 已过时,将导致静默失败,您的通知将不会显示.

Your alarms would be firing correctly as you are using explicit intents. But in Oreo/API26 .SetDefaults(NotificationDefaults.All is obsolete and will cause a silent failure and your notifications will not be displayed.

因此,您需要设置一个通知渠道,以正确显示您的通知以及所有花里胡哨的内容.

So you need to setup a notification channel to properly display your notification with all the bells and whistles.

如果您更换通知生成器 &通知代码:

If you replace your notification builder & notify code:

var builder = new Notification.Builder(context) .SetContentTitle(title) ~~~~ manager.Notify(1331, notification);

通过对 Oreo/API26(+) 的 API 检查,您可以为您的应用建立通知渠道:

With an API check for Oreo/API26(+) you can established an notification channel for your app:

using (var notificationManager = NotificationManager.FromContext(context)) { Notification notification; if (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.O) { notification = new Notification.Builder(context) .SetContentTitle(title) .SetContentText(message) .SetAutoCancel(true) .SetSmallIcon(Resource.Drawable.icon) .SetDefaults(NotificationDefaults.All) .SetContentIntent(pending) .Build(); } else { // Setup a NotificationChannel, Go crazy and make it public, urgent with lights, vibrations & sound. var myUrgentChannel = context.PackageName; const string channelName = "SushiHangover Urgent"; NotificationChannel channel; channel = notificationManager.GetNotificationChannel(myUrgentChannel); if (channel == null) { channel = new NotificationChannel(myUrgentChannel, channelName, NotificationImportance.High); channel.EnableVibration(true); channel.EnableLights(true); channel.SetSound( RingtoneManager.GetDefaultUri(RingtoneType.Notification), new AudioAttributes.Builder().SetUsage(AudioUsageKind.Notification).Build() ); channel.LockscreenVisibility = NotificationVisibility.Public; notificationManager.CreateNotificationChannel(channel); } channel?.Dispose(); notification = new Notification.Builder(context) .SetChannelId(myUrgentChannel) .SetContentTitle(title) .SetContentText(message) .SetAutoCancel(true) .SetSmallIcon(Resource.Drawable.icon) .SetContentIntent(pending) .Build(); } notificationManager.Notify(1331, notification); notification.Dispose(); }

现在在设置中,您的应用分配了一个频道,用户可以选择自定义频道:

Now in settings, your app has a channel assigned to it that the user can customize if they so choose:

更多推荐

Android 8.0(Oreo) 中的 AlarmManager 和通知

本文发布于:2023-11-27 04:30:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636602.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通知   Android   AlarmManager   Oreo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!