1

Am trying to make an Alarm notify on a scheduled time from my Sqlite, but the Alarm notifies only when i launch the Application, why is this

Below is how am Implementing my Alarm class:

public class AlarmImplementation 
    {
        public AlarmImplementation()
        {
        }


        public async void MakeAlarm()
        {
            MedicationDatabase db = new MedicationDatabase();
            //This method has the list of time in Sqlite
            var alarm_list = db.GetAlarmList();
            //Debug.WriteLine(" Time -- : "+ m.ToString());
            var message = "Hello Its me new one ";
            var title = "Diabetics App";
            //looping time from Sqlite 
            foreach (var list in alarm_list)
            {
                var hour = Int32.Parse(list.Substring(0, 2));
                var minute = Int32.Parse(list.Substring(3, 2));
                Debug.WriteLine("Hour : " + hour + "\n");
                Debug.WriteLine("Minute   " + minute + "\n");



                Intent myintent = new Intent(Xamarin.Forms.Forms.Context, typeof(AlarmReceiver));
                myintent.PutExtra("message", message);
                myintent.PutExtra("title", title);
                PendingIntent pendingintent = PendingIntent.GetBroadcast(Xamarin.Forms.Forms.Context, 0, myintent, PendingIntentFlags.UpdateCurrent);

                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);
                //  PendingIntent pendingIntent = PendingIntent.GetBroadcast(this, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
                AlarmManager alarmManager = Xamarin.Forms.Forms.Context.GetSystemService(Android.Content.Context.AlarmService) as AlarmManager;
                alarmManager.Set(AlarmType.RtcWakeup, cal.TimeInMillis, pendingintent);


            }
        }
    } 

Now when you look at my code above , I get time from my Sqlite then i wire it in the AlarmManager.

Then in the code below i think i call the MakeAlarm Method in the MainActivity.

This is my MainActivity class:

 public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
            //            ActionBar.Hide();
            RequestWindowFeature(WindowFeatures.NoTitle);

            base.OnCreate(bundle);

            global::Xamarin.Forms.Forms.Init(this, bundle);
            //Xamarin.Forms.Init();

            AlarmImplementation a = new AlarmImplementation();

            Task startupWork = new Task(() => { a.MakeAlarm(); });
            startupWork.Start();

            LoadApplication(new App());
        }

    }

My Question is how can i make an Alarm notify on a scheduled time without pressing a Button, since i have my time in the Sqlite.

  • You could refer to : https://stackoverflow.com/questions/42583532/android-notification-start-when-ever-i-open-app – York Shen Nov 01 '17 at 01:07

0 Answers0