Put reminder in real calendar on the phone?

2019-02-21 01:55发布

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 want it to appear in the calendar. How do I make this in Android?

1条回答
Melony?
2楼-- · 2019-02-21 02:13

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" />
查看更多
登录 后发表回答