Advertisement
MIkeKaye

MainApplication.cs

Feb 16th, 2025
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.54 KB | Source Code | 0 0
  1. using Android.App;
  2. using Android.Content;
  3. using Android.OS;
  4. using Android.Provider;
  5. using Android.Runtime;
  6.  
  7. namespace MauiApp5
  8. {
  9.     public static class MyHelper
  10.     {
  11.         public static void calendarHelper()
  12.         {
  13.             MainActivity.Instance.calendarHelper();
  14.         }
  15.     }
  16.     [Application]
  17.     public class MainApplication : MauiApplication
  18.     {
  19.         public MainApplication(IntPtr handle, JniHandleOwnership ownership)
  20.             : base(handle, ownership)
  21.         {
  22.         }
  23.  
  24.         protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
  25.     }
  26.     public partial class MainActivity : MauiAppCompatActivity
  27.     {
  28.         public static MainActivity Instance { get; private set; }
  29.         protected override void OnCreate(Bundle savedInstanceState)
  30.         {
  31.             base.OnCreate(savedInstanceState);
  32.             Instance = this;
  33.         }
  34.         public void calendarHelper()
  35.         {
  36.             long eventID = 221;
  37.             ContentResolver cr = ContentResolver;
  38.             ContentValues values = new ContentValues();
  39.             values.Put(CalendarContract.Reminders.InterfaceConsts.Minutes, 1);
  40.             values.Put(CalendarContract.Reminders.InterfaceConsts.EventId, eventID);
  41.             values.Put(CalendarContract.Reminders.InterfaceConsts.Method, (int)RemindersMethod.Alert);
  42.             Android.Net.Uri uri = cr.Insert(CalendarContract.Reminders.ContentUri, values);
  43.         }
  44.  
  45.         public void AddEventAt10AM(DateTime theTime, string MyMessage)
  46.         {
  47.             // Obtain the ContentResolver from the application context
  48.             var contentResolver = Android.App.Application.Context.ContentResolver;
  49.  
  50.             // Calculate the start and end times for 10AM local time
  51.             DateTime theTime10AM = theTime.Date.AddHours(10);
  52.             long startMillis = (long)(theTime10AM.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalMilliseconds;
  53.             long endMillis = startMillis + 60 * 60 * 1000; // 1-hour duration
  54.  
  55.             // IMPORTANT: You need a valid Calendar ID. You might have to query the Calendars table to get this.
  56.             // For simplicity, this example uses "1" but in a real scenario, you'll need to determine the correct calendar.
  57.             int calendarId = 1;
  58.  
  59.             ContentValues eventValues = new ContentValues();
  60.             eventValues.Put(CalendarContract.Events.Dtstart, startMillis);
  61.             eventValues.Put(CalendarContract.Events.Dtend, endMillis);
  62.             eventValues.Put(CalendarContract.Events.Title, MyMessage);
  63.             eventValues.Put(CalendarContract.Events.CalendarId, calendarId);
  64.  
  65.             // Insert the event
  66.             Android.Net.Uri eventUri = contentResolver.Insert(CalendarContract.Events.ContentUri, eventValues);
  67.  
  68.             // Optionally, add a reminder for the event (for example, alert 10 minutes before)
  69.             if (eventUri != null)
  70.             {
  71.                 // Extract the event ID from the URI
  72.                 long eventId = long.Parse(eventUri.LastPathSegment);
  73.  
  74.                 ContentValues reminderValues = new ContentValues();
  75.                 reminderValues.Put(CalendarContract.Reminders.Minutes, 10);
  76.                 reminderValues.Put(CalendarContract.Reminders.EventId, eventId);
  77.                 reminderValues.Put(CalendarContract.Reminders.Method, (int)CalendarContract.RemindersMethod.Alert);
  78.  
  79.                 Android.Net.Uri reminderUri = contentResolver.Insert(CalendarContract.Reminders.ContentUri, reminderValues);
  80.             }
  81.         }
  82.     }
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement