Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Android.App;
- using Android.Content;
- using Android.OS;
- using Android.Provider;
- using Android.Runtime;
- namespace MauiApp5
- {
- public static class MyHelper
- {
- public static void calendarHelper()
- {
- MainActivity.Instance.calendarHelper();
- }
- }
- [Application]
- public class MainApplication : MauiApplication
- {
- public MainApplication(IntPtr handle, JniHandleOwnership ownership)
- : base(handle, ownership)
- {
- }
- protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
- }
- public partial class MainActivity : MauiAppCompatActivity
- {
- public static MainActivity Instance { get; private set; }
- protected override void OnCreate(Bundle savedInstanceState)
- {
- base.OnCreate(savedInstanceState);
- Instance = this;
- }
- public void calendarHelper()
- {
- long eventID = 221;
- ContentResolver cr = ContentResolver;
- ContentValues values = new ContentValues();
- values.Put(CalendarContract.Reminders.InterfaceConsts.Minutes, 1);
- values.Put(CalendarContract.Reminders.InterfaceConsts.EventId, eventID);
- values.Put(CalendarContract.Reminders.InterfaceConsts.Method, (int)RemindersMethod.Alert);
- Android.Net.Uri uri = cr.Insert(CalendarContract.Reminders.ContentUri, values);
- }
- public void AddEventAt10AM(DateTime theTime, string MyMessage)
- {
- // Obtain the ContentResolver from the application context
- var contentResolver = Android.App.Application.Context.ContentResolver;
- // Calculate the start and end times for 10AM local time
- DateTime theTime10AM = theTime.Date.AddHours(10);
- long startMillis = (long)(theTime10AM.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalMilliseconds;
- long endMillis = startMillis + 60 * 60 * 1000; // 1-hour duration
- // IMPORTANT: You need a valid Calendar ID. You might have to query the Calendars table to get this.
- // For simplicity, this example uses "1" but in a real scenario, you'll need to determine the correct calendar.
- int calendarId = 1;
- ContentValues eventValues = new ContentValues();
- eventValues.Put(CalendarContract.Events.Dtstart, startMillis);
- eventValues.Put(CalendarContract.Events.Dtend, endMillis);
- eventValues.Put(CalendarContract.Events.Title, MyMessage);
- eventValues.Put(CalendarContract.Events.CalendarId, calendarId);
- // Insert the event
- Android.Net.Uri eventUri = contentResolver.Insert(CalendarContract.Events.ContentUri, eventValues);
- // Optionally, add a reminder for the event (for example, alert 10 minutes before)
- if (eventUri != null)
- {
- // Extract the event ID from the URI
- long eventId = long.Parse(eventUri.LastPathSegment);
- ContentValues reminderValues = new ContentValues();
- reminderValues.Put(CalendarContract.Reminders.Minutes, 10);
- reminderValues.Put(CalendarContract.Reminders.EventId, eventId);
- reminderValues.Put(CalendarContract.Reminders.Method, (int)CalendarContract.RemindersMethod.Alert);
- Android.Net.Uri reminderUri = contentResolver.Insert(CalendarContract.Reminders.ContentUri, reminderValues);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement