the calendar in Android, only the real machine, the simulation is not, so the test environment must be real machine!!
Because the calendar is the system comes with, so we read and write it must apply permission, that is, in Androidmanifest.xml add the following two lines of code (one to read a write):
<!--get calendar read and Write permission-- <uses-permission android:name= "android.permission.READ_ CALENDAR "/> <uses-permission android:name=" Android.permission.WRITE_CALENDAR "/>
Calendarthe schedule URL changes after the 2.2 version
In order to be compatible with different versions of the calendar, the URL changes after 2.2 The private static String Calandereventurl = ""; static {if (Integer.parseint (Build.VERSION.SDK) >= 8) {Calandereventurl = "content://com.android.calendar/events";} else {calandereventurl = "content://calendar/ Events ";}}
Here's the main code to get the schedule:
Contentresolver CR = Context.getcontentresolver ();//The Uriuri URI of the corresponding event in the calendar = Uri.parse (Calandereventurl); cursor cursor = cr.query (URI, NULL, NULL, NULL, NULL), while (Cursor.movetonext ()) {calendarevent CDE = new Calendarevent () ; cde.setcalendar_id (Cursor.getstring (Cursor.getcolumnindex (events.calendar_id))); Cde.settitle (cursor.getString (Cursor.getcolumnindex (Events.title))); Cde.setdtstart (Cursor.getstring (Cursor.getcolumnindex (Events.dtstart))) Cde.setdtend (Cursor.getString ( Cursor.getcolumnindex (Events.dtend)); Cde.setduration (Cursor.getstring (Cursor.getcolumnindex (Events.DURATION)) ); Cde.setrrule (Cursor.getstring (Cursor.getcolumnindex (Events.rrule)); Cde.seteventtimezone (Cursor.getString ( Cursor.getcolumnindex (Events.event_timezone)); Calendarevents.add (CDE);}
Here is the main code for writing the schedule
These fields are required for writing, so I've only written these fields.
Contentresolver CR = Context.getcontentresolver ();//The Uriuri URI of the corresponding event in the calendar = Uri.parse (Calandereventurl); cursor cursor = cr.query (URI, NULL, NULL, NULL, NULL), while (Cursor.movetonext ()) {calendarevent CDE = new Calendarevent () ; cde.setcalendar_id (Cursor.getstring (Cursor.getcolumnindex (events.calendar_id))); Cde.settitle (cursor.getString (Cursor.getcolumnindex (Events.title))); Cde.setdtstart (Cursor.getstring (Cursor.getcolumnindex (Events.dtstart))) Cde.setdtend (Cursor.getString ( Cursor.getcolumnindex (Events.dtend)); Cde.setduration (Cursor.getstring (Cursor.getcolumnindex (Events.DURATION)) ); Cde.setrrule (Cursor.getstring (Cursor.getcolumnindex (Events.rrule)); Cde.seteventtimezone (Cursor.getString ( Cursor.getcolumnindex (Events.event_timezone)); Calendarevents.add (CDE);}
Get and modify Android's own calendar schedule