当前位置: 首页 > 编程语言 > C#

每次打开app都会响分享

时间:2023-04-11 00:58:02 C#

每次打开app都会响我搞不懂为什么每次打开app我的notification设置为响alarmgoes取消。我只在特定的日期和时间设置日历。我的闹钟不仅没有在日历上指示的时间和星期几响起。我的闹钟需要维修吗?我试图确保即使应用程序关闭它也会关闭。下面的代码(MyService)在MainActivityonCreate()中被调用。[服务]publicclassMyService:Service{//constintNOTIFICATION_ID=9000;publicoverrideStartCommandResultOnStartCommand(Intentintent,StartCommandFlagsflags,intstartId){SetAlarm();返回StartCommandResult.Sticky;}privatevoidSetAlarm(){//设置日历Java.Util.Calendarcalendar=Java.Util.Calendar.Instance;calendar.Set(Java.Util.CalendarField.DayOfWeek,1);calendar.Set(Java.Util.CalendarField.HourOfDay,02);calendar.Set(Java.Util.CalendarField.Minute,15);AlarmManager管理器=(AlarmManager)GetSystemService(Context.AlarmService);意图经理意图;待定意图managerIntent=newIntent(this,typeof(AlarmNotificationReceiver));//pendingIntent=PendingIntent.GetBroadcast(this,0,managerIntent,0);pendingIntent=PendingIntent.GetBroadcast(this,0,managerIntent,PendingIntentFlags.UpdateCurrent);//manager.SetRepeating(AlarmType.RtcWakeup,SystemClock.ElapsedRealtime()+3000,60+1000,待定意图);//manager.SetRepeating(AlarmType.ElapsedRealtimeWakeup,SystemClock.ElapsedRealtime()+AlarmManager.IntervalHalfHour,AlarmManager.IntervalHalfHour,pendingIntent);//manager.SetRepeating(AlarmType.RtcWakeup,calendar.TimeInMillis,AlarmManager.IntervalHalfHour,pendingIntent);manager.SetRepeating(AlarmType.RtcWakeup,calendar.TimeInMillis,604800,pendingIntent);}[BroadcastReceiver(Enabled=true)]publicclassAlarmNotificationReceiver:BroadcastReceiver{publicoverridevoidOnReceive(Contextcontext,Intentintent){NotificationCompat.Builderbuilder=newNotificationCompat.Builder(context);builder.SetAutoCancel(true).SetDefaults((int)NotificationDefaults.All).SetSmallIcon(Resource.Drawable.Icon).SetContentTitle("ComicPullList").SetStyle(newNotificationCompat.BigTextStyle().BigText("新漫画有这周出来了!检查你的列表,如果需要的话拉漫画。”)).SetContentText(“新漫画已经出来了星期!检查你的列表,如果需要的话拉漫画。”);NotificationManagermanager=(NotificationManager)context.GetSystemService(Context.NotificationService);manager.Notify(1,builder.Build());}}}我添加的新代码如下:AlarmManagermanager=(AlarmManager)GetSystemService(Context.AlarmService);//设置日历Java.Util.Calendarcalendar=Java.Util.Calendar.Instance;if(calendar.Get(Java.Util.CalendarField.DayOfWeek)==Java.Util.Calendar.Wednesday){calendar.Add(Java.Util.CalendarField.Date,4);}while(calendar.Get(Java.Util.CalendarField.DayOfWeek)!=Java.Util.Calendar.Wednesday){日历。添加(Java.Util.CalendarField.Date,4);}//calendar.Set(Java.Util.CalendarField.DayOfWeek,1);calendar.Set(Java.Util.CalendarField.HourOfDay,15);calendar.Set(Java.Util.CalendarField.Minute,30);意图managerIntent;PendingIntentpendingIntent;managerIntent=newIntent(this,typeof(AlarmNotificationReceiver));//pendingIntent=PendingIntent.Get广播(这,0,managerIntent,0);pendingIntent=PendingIntent.GetBroadcast(this,0,managerIntent,PendingIntentFlags.UpdateCurrent);//manager.SetRepeating(AlarmType.RtcWakeup,SystemClock.ElapsedRealtime()+3000,60+1000,pendingIntent);//manager.SetRepeating(AlarmType.ElapsedRealtimeWakeup,SystemClock.ElapsedRealtime()+AlarmManager.IntervalHalfHour,AlarmManager.IntervalHalfHour,pendingIntent);//manager.SetRepeating(AlarmType.RtcWakeup,calendar.TimeInMillis,AlarmManager.Intervaling);manager.SetRepeating(AlarmType.RtcWakeup,calendar.TimeInMillis,1000*60*60*24*7,pendingIntent);您正在设置过去的第一个警报如果您指定的触发时间是过去,警报将立即触发。您需要将日期设置为下一个星期日。以上是C#学习教程:每次打开应用时闹钟都会关闭所有分享的内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注——例子:using(AlarmManagermanager=(AlarmManager)GetSystemService(AlarmService))using(varcalendar=Calendar.Instance){如果(calendar.Get(CalendarField.DayOfWeek)==Calendar.Sunday)calendar.Add(CalendarField.Date,1);while(calendar.Get(CalendarField.DayOfWeek)!=Calendar.Sunday)calendar.Add(CalendarField.Date,1);calendar.Set(CalendarField.HourOfDay,02);calendar.Set(CalendarField.Minute,15);Log.Debug("SO",$"当前日期是:{Calendar.Instance.Time.ToString()}");Log.Debug("SO",$"Alarmwillfireat{calendar.Time.ToString()}");varmanagerIntent=newIntent(this,typeof(MainActivity));varpendingIntent=PendingIntent.GetBroadcast(this,0,managerIntent,PendingIntentFlags.UpdateCurrent);manager.SetRepeating(AlarmType.RtcWakeup,calendar.TimeInMillis,1000*60*60*24*7,pendingIntent);}输出:[SO]当前日期是:2017年8月12日星期六23:01:11PDT[SO]警报将在:SunAug1302:15:11PDT2017本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: