xamarin android中调度AlarmManager的问题

xamarin android中调度AlarmManager的问题,android,xamarin,alarmmanager,Android,Xamarin,Alarmmanager,我想每天20点设置一次闹钟, 当我在模拟器中调试应用程序时,一切正常,但在我的手机中,有时正常,有时不正常。 这是我的密码 public class AlarmUtility { private static TimeSpan AlarmTime = new TimeSpan(20, 00, 0); private static long CurrentAlarm; public static void StartAlarm() {

我想每天20点设置一次闹钟, 当我在模拟器中调试应用程序时,一切正常,但在我的手机中,有时正常,有时不正常。 这是我的密码

 public class AlarmUtility
{
    private static TimeSpan AlarmTime = new TimeSpan(20, 00, 0);
    private static long CurrentAlarm;

    public static void StartAlarm()
    {          
        AlarmManager manager = (AlarmManager)Application.Context.GetSystemService(Context.AlarmService);
        Intent myIntent;
        PendingIntent pendingIntent;
        myIntent = new Intent(Application.Context, typeof(DailyNotificationReciever));
        pendingIntent = PendingIntent.GetBroadcast(Application.Context, 0, myIntent, PendingIntentFlags.UpdateCurrent);

        long date;

        //check if alarm date has passed
        if(DateTime.Now.TimeOfDay < AlarmTime)
        {
            date = getAlarmJavaTime(false);
        }
        else
        {
            date = getAlarmJavaTime(true);
        }

        if (CurrentAlarm != date)
        {
            manager.SetInexactRepeating(AlarmType.RtcWakeup, date, AlarmManager.IntervalDay, pendingIntent);
            CurrentAlarm = date;
        }

    }
   private static long getAlarmJavaTime(bool addDay)
    {
        Calendar cal = Calendar.Instance;
        cal.TimeInMillis = Java.Lang.JavaSystem.CurrentTimeMillis();
        cal.Set(CalendarField.HourOfDay, AlarmTime.Hours);
        cal.Set(CalendarField.Minute, AlarmTime.Minutes);
        cal.Set(CalendarField.Second, AlarmTime.Seconds);
        if (addDay)
        {
            cal.Add(CalendarField.DayOfMonth, 1);
        }

        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss 'GMT'Z yyyy");
        string a = dateFormat.Format(cal.Time);
        return cal.TimeInMillis;
    }       
}

谢谢

因为广播接收器是应用程序的一个组件,如果应用程序停止,它将不会收到任何警报或事件。这就是为什么你的代码有时有效,有时无效的可能原因。此外,就代码级别而言,我没有发现你的代码有任何问题

如果要检查这种可能性,可以使用以下adb命令:

adb shell dumpsys package "com.package.name" and check "stopped=true" 
检查应用程序是否处于停止状态


如果得到证实,我只能说,这种行为是故意的。

有几件事你应该知道:

1设备重新启动后,所有警报均已清除,您必须重新安排其时间:

2如果您使用与您相同的PendingEvent计划报警-相同的请求代码、空意图和相同的标志,则此报警将覆盖上一个报警


试着根据以上信息分析你的问题

粘贴在此处您的DailyNotificationReceiver将DailyNotificationReceiver代码添加到原始邮件中,并请告诉我当您使用StartArm功能时,电子邮件的位置。您好,根据您所说的,也许用服务更换广播接收器将解决问题problem@royi,不是那么简单,如果您的问题是让应用程序在后台保持活动状态,那么应该有很多博客等等,您可以搜索它。最后,如果你觉得这个答案有帮助,请你在这个答案上做标记好吗?谢谢!
adb shell dumpsys package "com.package.name" and check "stopped=true"