AlarmManager在Xamarin.Android中激活了过去的时间

时间:2018-03-10 09:58:51

标签: android xamarin xamarin.forms xamarin.android alarmmanager

这是我的警报管理器类代码

   public void Remind(int hour ,int minute, string title, string message,int day)
        {
            Intent alarmIntent = new Intent(Forms.Context, typeof(MainActivity.AlarmReceiver));
             alarmIntent.PutExtra("message", message);
            alarmIntent.PutExtra("title", title);
            int x = DateTime.Now.Millisecond;
            PendingIntent pendingIntent = PendingIntent.GetBroadcast(Forms.Context, DateTime.Now.Millisecond, alarmIntent, PendingIntentFlags.OneShot);
            Java.Util.Date date = new Java.Util.Date();
            Java.Util.Calendar cal = Java.Util.Calendar.Instance;

            cal.TimeInMillis = Java.Lang.JavaSystem.CurrentTimeMillis();

            cal.Set(Java.Util.CalendarField.HourOfDay, hour);
            cal.Set(Java.Util.CalendarField.Minute, minute);
            cal.Set(Java.Util.CalendarField.Second, 0);
            cal.Set(Java.Util.CalendarField.DayOfMonth, day);


            AlarmManager alarmManager = Xamarin.Forms.Forms.Context.GetSystemService(Android.Content.Context.AlarmService) as AlarmManager;
            alarmManager.Set(AlarmType.RtcWakeup, cal.TimeInMillis, pendingIntent);


        }

MainActivity中的BroadcastReceiver;

[BroadcastReceiver]
    public class AlarmReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {

            var message = intent.GetStringExtra("message");
            var title = intent.GetStringExtra("title");

            var notIntent = new Intent(context, typeof(MainActivity));

            var contentIntent = PendingIntent.GetActivity(context, DateTime.Now.Millisecond, notIntent, PendingIntentFlags.OneShot);
            var manager = Android.Support.V4.App.NotificationManagerCompat.From(context);

            var style = new Android.Support.V4.App.NotificationCompat.BigTextStyle();
            style.BigText(message);

            int resourceId;

            resourceId = Resource.Drawable.icon;

            var wearableExtender = new Android.Support.V4.App.NotificationCompat.WearableExtender()
    .SetBackground(Android.Graphics.BitmapFactory.DecodeResource(context.Resources, resourceId))
                ;

            //Generate a notification with just short text and small icon



            var builder = new Android.Support.V7.App.NotificationCompat.Builder(context)
                            .SetContentIntent(contentIntent)
                                     .SetSmallIcon(Resource.Drawable.icon)
                            .SetContentTitle(title)
                            .SetContentText(message)
                            .SetStyle(style)
                            .SetWhen(Java.Lang.JavaSystem.CurrentTimeMillis())
                            .SetAutoCancel(true)
                            .Extend(wearableExtender);

            var notification = builder.Build();
            manager.Notify(DateTime.Now.Millisecond, notification);
        }
    }

}

如果我选择在未来一小时/分钟启动闹钟,则代码效果很好。

该应用程序包含五个alarmChange每日

如果我打开应用程序..在这些时间之前一切都非常好并且警报实时发射..但如果我打开它在五个任何时间之后...所有过去的警报开火

示例:

现在是15:00,我将闹钟设置为15:45,闹钟在15:45关闭,一切正常

现在是15:00,我将闹钟设置为14:30,打开应用程序时闹钟响起

0 个答案:

没有答案