处理闹钟+通知中的设定时间列表

时间:2017-11-07 12:44:17

标签: c# android xamarin notifications alarmmanager

我试图安排一个包含时间列表的通知,假设我想安排以下时间:

1. 22:00
2. 10:00
3. 17:00

这是我解决它的方式,

A)。首先我将时间存储在Sqlite

B)。 然后我用each loop确保我在预定的经理中插入时间列表,但通知没有到来。

  

即使我试图在console打印,也表明时间已经到了   可用,但我想知道为什么通知无效。

我的代码如下:

   public void MyLooper()
        {
            try
            {
                alarmService = new SetAlarmImplementation();
                MedicationDatabase c = new MedicationDatabase();
                //get schedulled time list
                var alarm = c.GetAlarmList();
                var username = c.GetUserName();
                var hour = 0;
                var minute = 0;

                foreach (var list in alarm)
                {
                    // This is the time i pick from the list
                    hour = Int32.Parse(list.Substring(0, 2));
                    minute = Int32.Parse(list.Substring(3, 2));

                    try
                    {
                        System.Diagnostics.Debug.WriteLine("Hour : " + hour + " Minutes : " + minute);

                        alarmService.SetAlarm(hour, minute, "Diabetics App", "Hello  " + username + " , I remind you to take your insulin at this stated time");
                    }
                    catch (Exception e)
                    {
                        System.Diagnostics.Debug.WriteLine("Error  " + e);
                    }

                }
            }
            catch (Exception f)
            {

            }

        }

但令人惊讶的是,如果我在这样的代码中直接插入时间,它可以正常工作:

alarmService.SetAlarm(13, 14, "Diabetics App", "Hello  " + username + " , I remind you to take your insulin at this stated time");
  

如何在闹钟管理器中注册所有存储的时间?

修改

警报实施:

namespace Diabetes.Droid
{
    public class SetAlarmImplementation : ISetAlarm
    {

        public SetAlarmImplementation() { }


        public void SetAlarm(int hour, int minute, string title, string message)
        {


            Intent myintent = new Intent(Android.App.Application.Context, typeof(AlarmReceiver));
            myintent.PutExtra("message", message);
            myintent.PutExtra("title", title);
            int _id = DateTime.Now.Millisecond;


            TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
            long millis = (long)ts.TotalMilliseconds;
            int i = (int)millis;

            PendingIntent pendingintent = PendingIntent.GetBroadcast(Android.App.Application.Context, i, myintent, 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);



            AlarmManager alarmManager = Android.App.Application.Context.GetSystemService(Android.Content.Context.AlarmService) as AlarmManager;
            alarmManager.Set(AlarmType.RtcWakeup, cal.TimeInMillis, pendingintent);
        }


    }
}

我的闹钟接收器?

using System;
using Android.App;
using Android.Content;
using Android.Media;
using Android.Support.V4.App;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using Android.Widget;
using Diabetes.localDB;
using Xamarin.Forms;

namespace Diabetes.Droid
{
    [BroadcastReceiver]
    [IntentFilter(new string[] { "android.intent.action.BOOT_COMPLETED" }, Priority = (int)IntentFilterPriority.LowPriority)]
    public class AlarmReceiver : BroadcastReceiver
    {



        public override void OnReceive(Context context, Intent intent)
        {
            var message = intent.GetStringExtra("message");
            var title = intent.GetStringExtra("title");
            Intent i = new Intent(context, typeof(AppStickyService));
            i.PutExtra("message", message);
            i.PutExtra("title", title);
            context.StartService(i);

        }
    }

    [BroadcastReceiver]
    [IntentFilter(new string[] { "TAKE", "SKIP" })]
    public class CustomActionReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {

            switch (intent.Action)
            {
                case "TAKE":
                    try
                    {
                        MedicationDatabase db = new MedicationDatabase();
                        db.addtracktaken("true");
                        Toast.MakeText(context, "DOSAGE TAKEN", ToastLength.Short).Show();
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine(e.StackTrace);
                    }
                    break;
                case "SKIP":
                    try
                    {
                        Toast.MakeText(context, "DOSAGE SKIPPED", ToastLength.Short).Show();
                        MedicationDatabase db = new MedicationDatabase();
                        db.addtrackmissed("true");
                        Toast.MakeText(context, "DOSAGE MISSED", ToastLength.Short).Show();
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine(e.StackTrace);
                    }
                    break;
            }


        }
    }
}

然后是服务类

using System;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget;
using Java.Util;

namespace Diabetes.Droid
{

    [Service(Exported = true, Name = "com.diabetics.Diabetes.AppStickyService")]
    public class AppStickyService : IntentService
    {

        public override void OnCreate()
        {
            base.OnCreate();
            Toast.MakeText(this, "service started ", ToastLength.Long).Show();

            System.Diagnostics.Debug.WriteLine("Sticky Service - Created");
        }

        public override StartCommandResult OnStartCommand(Android.Content.Intent intent, StartCommandFlags flags, int startId)
        {

            WireAlarm(intent);

            return StartCommandResult.Sticky;
        }

        public override Android.OS.IBinder OnBind(Android.Content.Intent intent)
        {
            System.Diagnostics.Debug.WriteLine("Sticky Service - Binded");
            Toast.MakeText(this, "Service started", ToastLength.Long).Show();

            return null;
        }

        public override void OnDestroy()
        {
            try
            {
                base.OnDestroy();

            }
            catch (Java.Lang.IllegalStateException ex)
            {
                //Log.Debug("MainActivity.OnDestroy", ex, "The activity was destroyed twice");
                System.Diagnostics.Debug.WriteLine("Sticky Service error "+ ex);
            }



        }


        private void cancelnotification(int id)
        {
            NotificationManager notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
            notificationManager.Cancel(id);
        }

        public void SetAlarm(int hour, int minute, string title, string message)
        {

            //AppContext.StartService(new Intent(AppContext, typeof(AppStickyService)));
            Intent myintent = new Intent(Android.App.Application.Context, typeof(AppStickyService));
            myintent.PutExtra("message", message);
            myintent.PutExtra("title", title);
            //PendingIntent pendingintent = PendingIntent.GetBroadcast(Android.App.Application.Context, 0, myintent, PendingIntentFlags.UpdateCurrent);
            PendingIntent pintent = PendingIntent.GetService(this, 0, new Intent(this, typeof(AppStickyService)), 0);

            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);
            AlarmManager alarmManager = Android.App.Application.Context.GetSystemService(Android.Content.Context.AlarmService) as AlarmManager;
            alarmManager.Set(AlarmType.RtcWakeup, cal.TimeInMillis, pintent);
            Toast.MakeText(this, "Service started ", ToastLength.Long).Show();
        }


        public void WireAlarm(Intent intent)
        {
            //Show toast here
            //Toast.MakeText(context, "Hello it's me ", ToastLength.Short).Show();
            var message = intent.GetStringExtra("message");
            var title = intent.GetStringExtra("title");

            //Create intent for action 1 (TAKE)
            var actionIntent1 = new Intent();
            actionIntent1.SetAction("TAKE");
            var pIntent1 = PendingIntent.GetBroadcast(Android.App.Application.Context, 0, actionIntent1, PendingIntentFlags.CancelCurrent);

            //Create intent for action 2 (REPLY)
            var actionIntent2 = new Intent();
            actionIntent2.SetAction("SKIP");
            var pIntent2 = PendingIntent.GetBroadcast(Android.App.Application.Context, 0, actionIntent2, PendingIntentFlags.CancelCurrent);

            Intent resultIntent = Android.App.Application.Context.PackageManager.GetLaunchIntentForPackage(Android.App.Application.Context.PackageName);

            var contentIntent = PendingIntent.GetActivity(Android.App.Application.Context, 0, resultIntent, PendingIntentFlags.CancelCurrent);

            var pending = PendingIntent.GetActivity(Android.App.Application.Context, 0,
                resultIntent,
                PendingIntentFlags.CancelCurrent);

            //Debug.WriteLine(" Time -- : "+ m.ToString());


            // Instantiate the Big Text style:
            Notification.BigTextStyle textStyle = new Notification.BigTextStyle();


            var builder =
                new Notification.Builder(Android.App.Application.Context)
                                .AddAction(Resource.Drawable.tick_notify, "TAKE", pIntent1)
                                .AddAction(Resource.Drawable.cancel_notify, "SKIP", pIntent2)
                                .SetSmallIcon(Resource.Drawable.ic_launcher)
                                .SetContentTitle("Diabetics Reminder")
                                .SetDefaults(NotificationDefaults.Sound)
                                .SetStyle(new Notification
                                .BigTextStyle()
                                .SetSummaryText("")
                                .SetBigContentTitle(title)
                                .BigText(message)
             ).SetDefaults(NotificationDefaults.All);

            builder.SetContentIntent(pending);

            var notification = builder.Build();


            var manager = NotificationManager.FromContext(Android.App.Application.Context);
            manager.Notify(10010, notification);
        }

        protected override void OnHandleIntent(Intent intent)
        {
            throw new NotImplementedException();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我无法看到你的警报服务的实现,但问题可能是,为了在Android中有多个警报,每个警报需要一个唯一的标识符。因此,您可能会在代码中的每次迭代循环中覆盖相同的警报。有关详细信息,请参阅this answer