Put reminder in real calendar on the phone?

后端 未结 1 1497
隐瞒了意图╮
隐瞒了意图╮ 2021-01-17 05:16

I want to put reminders on the phone\'s calendar. In other words, reminders needs to be in the real calendar of the phone. We can create reminders with AlarmManager but I wa

相关标签:
1条回答
  • 2021-01-17 06:05

    Add this code in the button click or where ever you want to add reminder to calendar

    Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this) + "events");
                    ContentResolver cr = getContentResolver();
    
                    ContentValues values = new ContentValues();
    
                    try 
                    {
                        epoch = new java.text.SimpleDateFormat ("yyyy-MM-dd hh:mm a").parse(date+" "+time).getTime();
                        //epoch=epoch;
                        Log.e("epoch",String.valueOf(epoch));
                        epoch1 = new java.text.SimpleDateFormat ("yyyy-MM-dd hh:mm a").parse(date+" "+time).getTime();
                        //epoch1=epoch1;
                        Log.e("epoch1",String.valueOf(epoch1));
                    } catch (ParseException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
    
                    values.put("calendar_id", 1);
                    values.put("title", "Appoitment");
                    values.put("allDay", 0);
                    values.put("dtstart",epoch); // event starts at 11 minutes from now
                    values.put("dtend", epoch1 ); // ends 60 minutes from now
                    values.put("description", "Your consulting date and time ");
                    values.put("visibility", 0);
                    values.put("hasAlarm", 1);
                    if(EVENTS_URI!=null)
                    {
                    event1 = cr.insert(EVENTS_URI, values);
                    }
    
                    // reminder insert
                    Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this) + "reminders");
                    values = new ContentValues();
                    values.put( "event_id", Long.parseLong(event1.getLastPathSegment()));
                    values.put( "method", 1 );
                    values.put( "minutes", 10 );
                    if(REMINDERS_URI!=null)
                    {   
                    cr.insert( REMINDERS_URI, values );
                    }
                    alertDialog.setTitle("Event Saved");
                    Dismiss();
                    alertDialog.show();
    

    add "getCalendarUriBase" function to your code.

    private String getCalendarUriBase(Activity act) {
    
                    String calendarUriBase = null;
                    Uri calendars = Uri.parse("content://calendar/calendars");
                    Cursor managedCursor = null;
                    try {
                        managedCursor = act.managedQuery(calendars, null, null, null, null);
                    } catch (Exception e) {
                    }
                    if (managedCursor != null) {
                        calendarUriBase = "content://calendar/";
                    } else {
                        calendars = Uri.parse("content://com.android.calendar/calendars");
                        try {
                            managedCursor = act.managedQuery(calendars, null, null, null, null);
                        } catch (Exception e) {
                        }
                        if (managedCursor != null) {
                            calendarUriBase = "content://com.android.calendar/";
                        }
                    }
                    return calendarUriBase;
    

    Note:

    epoch = new java.text.SimpleDateFormat ("yyyy-MM-dd hh:mm a").parse(date+" "+time).getTime();
    

    In this line of code date and time must be your date as well time to which you want to add remindar.

    Permissions:

    <uses-permission android:name="android.permission.READ_CALENDAR" />
        <uses-permission android:name="android.permission.WRITE_CALENDAR" />
    
    0 讨论(0)
提交回复
热议问题