This is when I am doing the project to summarize some of the experience of adding events to the system calendar, I hope to help you, the following is mainly for the repeated addition of events to the calendar for analysis, which requires attention to the matter.
- The first is the main Android calendar three URLs, respectively, the calendar URL, event URL, event reminder URL, three URLs before android2.1 is as follows:
- Calanderurl = "Content://calendar/calendars";
- Calandereventurl = "Content://calendar/events";
- Calanderremiderurl= "Content://calendar/reminders";
However, after the Android2.2 version, three URLs became the following:
- Calanderurl = "Content://com.android.calendar/calendars";
- Calandereventurl = "Content://com.android.calendar/events";
- Calanderremiderurl = "Content://com.android.calendar/reminders"
- For example, you want to add a simple reminder event to the system calendar, which only corresponds to a reminder, you just have to put these settings in it,
Calendar cal = Calendar.getinstance ();
Args.put ("Dtstart", Data.getdatetime ()-1);
String Tt=utils.getdatefmtfromtime (Data.getdatetime ());
Args.put ("Dtend", Data.getdatetime ());
Args.put ("title", Data.getremindname ());
Args.put ("description", Data.getremindname ());
Args.put ("Needstartapplication", false);
String result = Calendarutil.addevent (this, args);//Add to Calendar
- However, when you add an event, but this event corresponds to multiple reminders, it needs to be added to the calendar repeatedly, it is necessary to use the calendar in the Events table Rrule add duplicate events, this rrule rule for different models of the phone, the effect of the display is not the same, Rrule format://rfc-5545 format freq=daily;byhour=8,13,18;byminute=0;bysecond=0;bydate=05 02,0922, I started with rrule to repeat adding events to the calendar. , the code is as follows:
Args.put ("Dtend", Entity.getendtakemedicinedate ());
Args.put ("Dtstart", Entity.getstarttakemedicinedate ());
Int days=convertutil.comparedate (Entity.getstarttakemedicinedate (), Entity.getendtakemedicinedate ());
int counttime= (entity.gettakemedicinecycle () +1) *days;
Switch (i) {
case 0:
args.put ("Rrule", String.Format ("freq=yearly; count=%s; byhour=%s; byminute=%s ", Counttime,entity.getfirsttakemedicinetime () [0],entity.getfirsttakemedicinetime () [1]);
break;
case 1:
args.put ("Rrule", String.Format ("freq=yearly; count=%s; byhour=%s; byminute=%s ", Counttime,entity.getsecondtakemedicinetime () [0],entity.getsecondtakemedicinetime () [1]);
break;
Case 2:
Args.putcishi String.Format ("freq=yearly; count=%s; byhour=%s; byminute=%s ", Counttime,entity.getthirdtakemedicinetime () [0],entity.getthirdtakemedicinetime () [1]);
break;
default:
break;
}
Args.put (Reminders.minutes, 5);
Args.put ("title", Entity.getmedicinename ());
Args.put ("description", String.Format ("%s%s%s%s", GetString (R.string.the_one_day_take_medicine), Entity.gettakemedicinecycle () +1,getstring (R.string.the_amount_of_time), Entity.getnumofeachtime ());
Args.put ("Needstartapplication", false);
- But on the Samsung model is correct, in other models are have write problems, added to the calendar of the number of bars and set the number of lines inconsistent, there is a bit of confusion, may be I write Rrule format is wrong, in this hope there is great God can consult, for this question whether there is a unified rrule format, In the case of not finding the same format, I used a somewhat reluctant method to achieve the function, if: To add a reminder, start time and end time randomly set, the daily reminder time point is also with the user, if there is a personal set from 2015/5/19-2015/5/25 every morning 09:0 0, 04:30, because the use of rrule rules added to the calendar display is not correct, so the following love is my own in the project does not use the Rrule rules of the correct method,
for (int i = 0; I <entity.gettakemedicinecycle () +1; i++) {
Jsonobject args=new jsonobject ();
TODO Building Standard Parameters
try {
Long Starttime=entity.getstarttakemedicinedate ();//Start date
Long endtime= entity.getendtakemedicinedate ();//End Date
switch (i) {
Case 0:
Combine the date and time of the start reminder with the calendar.
Calendar starttaketime1=calendar.getinstance ();
Starttaketime1.settimeinmillis (StartTime);
Starttaketime1.set (Calendar.hour, Entity.getfirsttakemedicinetime () [0]);//Start point of time
Starttaketime1.set (Calendar.minute, Entity.getfirsttakemedicinetime () [1]);//start of the minute
Calendar endtaketime1=calendar.getinstance ();
Endtaketime1.settimeinmillis (EndTime);
Endtaketime1.set (Calendar.hour, Entity.getfirsttakemedicinetime () [0]);
Endtaketime1.set (Calendar.minute, Entity.getfirsttakemedicinetime () [1]);
Args.put ("Dtstart", Starttaketime1.gettimeinmillis ());
Args.put ("Dtend", Endtaketime1.gettimeinmillis ());
Args.put ("Dtend", Endtaketime1.gettimeinmillis ());
Break
Case 1:
Calendar starttaketime2=calendar.getinstance ();
Starttaketime2.settimeinmillis (StartTime);
Starttaketime2.set (Calendar.hour, Entity.getsecondtakemedicinetime () [0]);
Starttaketime2.set (Calendar.minute, Entity.getsecondtakemedicinetime () [1]);
Calendar endtaketime2=calendar.getinstance ();
Endtaketime2.settimeinmillis (EndTime);
Endtaketime2.set (Calendar.hour, Entity.getsecondtakemedicinetime () [0]);
Endtaketime2.set (Calendar.minute, Entity.getsecondtakemedicinetime () [1]);
Args.put ("Dtstart", Starttaketime2.gettimeinmillis ());
Args.put ("Dtend", Endtaketime2.gettimeinmillis ());
Break
Case 2:
String Thrid=string.format ("%s:%s", Entity.getthirdtakemedicinetime () [0],entity.getthirdtakemedicinetime () [1]);
Calendar starttaketime3=calendar.getinstance ();
Starttaketime3.settimeinmillis (StartTime);
Starttaketime3.set (Calendar.hour, Entity.getthirdtakemedicinetime () [0]);
Starttaketime3.set (Calendar.minute, Entity.getthirdtakemedicinetime () [1]);
Calendar endtaketime3=calendar.getinstance ();
Endtaketime3.settimeinmillis (EndTime);
Endtaketime3.set (Calendar.hour, Entity.getthirdtakemedicinetime () [0]);
Endtaketime3.set (Calendar.minute,entity.getthirdtakemedicinetime () [1]);
Args.put ("Dtstart", Starttaketime3.gettimeinmillis ());
Args.put ("Dtend", Endtaketime3.gettimeinmillis ());
Break
Default
Break
}
Args.put (Reminders.minutes, 5);//Reminder 5 minutes
Args.put ("title", Entity.getmedicinename ());
Args.put ("description", String.Format ("%s%s%s%s", GetString (R.string.the_one_day_take_medicine), Entity.gettakemedicinecycle () +1,getstring (R.string.the_amount_of_time), Entity.getnumofeachtime ());
Args.put ("Needstartapplication", false);
Add Event to Calendar
String result=calendarutil.addevent (context, args);
}
/**
* Add event to Calendar
*
* @param context
* @param args
* Parameters
*/
public static String Addevent (context context, Jsonobject args) {
String result = null;
if (args!=null) {
Long CalendarID = Getcalendarid (context);
if (CalendarID = =-1) {
No calendar account; React meaningfully
return null;
}
Contentvalues values = new Contentvalues ();
Long Starttime=system.currenttimemillis ();
if (Args.has ("Dtstart")) {
StartTime = long.valueof (Jsonutil.getjsonobjectvalue (args, "Dtstart"). toString ());
Values.put ("Dtstart", startTime);
}
Long Endtime=-1;
if (Args.has ("Dtend")) {
EndTime = long.valueof (Jsonutil.getjsonobjectvalue (args, "dtend"). toString ());
Values.put ("Dtend", endTime);
}
Add Event
if (Args.has ("Rrule")) {
String id=events._id;
Values.put ("Rrule", Jsonutil.getjsonobjectvalue (args, "Rrule"). toString ());
}
if (Args.has ("duration")) {
Values.put ("duration", Jsonutil.getjsonobjectvalue (args, "duration"). toString ());
}
Values.put (Events.event_timezone, Timezone.getdefault (). GetID ());
Values.put (Events.event_end_timezone, Timezone.getdefault (). GetID ());
if (Args.has ("title")) {
Values.put ("title", Jsonutil.getjsonobjectvalue (args, "title"). ToString ());
}
Values.put (events.calendar_id, CalendarID);
if (Args.has ("description")) {
Values.put ("description", Jsonutil.getjsonobjectvalue (args, "description"). ToString ());
}
Uri uri = Context.getcontentresolver (). Insert (Uri.parse (calandereventurl), values);
Long eventId = long.valueof (Uri.getlastpathsegment ());
Values.clear ();
Add reminders
Values.put (reminders.event_id, eventId);
if (Args.has (reminders.minutes)) {
String id=reminders._id;
Values.put (Reminders.minutes,jsonutil.getjsonobjectvalue (args, reminders.minutes). toString ());
}
Values.put (Reminders.method, Reminders.method_alert);
Uri Reminderuri = Context.getcontentresolver (). Insert (Uri.parse (calanderremiderurl), values);
Long Reminderid = long.valueof (Reminderuri.getlastpathsegment ());
if (reminderid>0) {
if (Args.has ("Needstartapplication")) {
Boolean needstartapplication=boolean.valueof (Jsonutil.getjsonobjectvalue (args, "needstartapplication"). ToString ( ));
if (needstartapplication) {
Fzzsapplication.getinstance (). Tootheractivity (Context, constants.to_remind_to_splash_activity, endTime);
}
}
try {
Jsonobjectresultobj=new Jsonobject ();
Resultobj.put ("CalendarID", CalendarID);
Resultobj.put ("EventId", eventId);
Resultobj.put ("Reminderid", Reminderid);
Result=resultobj.tostring ();//Return three IDs
} catch (Exception e) {
}
}
}
return result;
}
- This is successfully added to the system calendar. For the addition, it is only for adding duplicate events to note, because that rrule format is a bit difficult to master, but also can not be set rrule rules, and as I do the combination of repeated additions. Also for the deletion of the repeatedly added events, an event corresponding to multiple reminders, his EventID more than one, but a reminder to a EventID, if you want to delete the repetition of the calendar added to the event, you need to save each EventID, This allows the loop to be removed according to EventID .
An overview of Android's experience in reading and writing system calendars