Hello everyone, I haven't updated my blog for a long time. Today I want to share with you some calendar operations in Android. Here I mainly use contentproiver. if you do not understand the contentprovider, we recommend that you first check the information and know what it is. This makes the following example easier.
Well, let's not talk about it. Here's a wake-up, calendar in Android,Only real machines are available, but there is no simulation. Therefore, the test environment must be real machines !!
Because the calendar is provided by the system, you must apply for permissions to read and write it. That is, add the following two lines of code (one read and one write) to androidmanifest. xml ):
<Uses-Permission Android: Name = "android. Permission. read_calendar"/> <br/> <uses-Permission Android: Name = "android. Permission. write_calendar"/>
In Android, calendar uses three urls: Calendar user URL, event URL, and event reminder URL. The three URLs are shown below before android2.1:
Calandreurl = "content: // calendar/calendars"; <br/> calandreeventurl = "content: // calendar/events"; <br/> calandreremiderurl = "content: // calendar/reminders ";
However, after android2.2, the three URLs have changed to the following:
Calandreurl = "content: // COM. android. calendar/calendars "; <br/> calandreeventurl =" content: // COM. android. calendar/events "; <br/> calandreremiderurl =" content: // COM. android. calendar/reminders ";
I wrote a simple demo to help you better understand it. You can follow the steps below:
Step 1: Create an android project named calendardemo.
Step 2: Modify the main. xml layout file and add three buttons. The Code is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <textview <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "@ string/Hello" <br/> <button <br/> Android: id = "@ + ID/readuserbutton" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "get a user" <br/> <button <br/> Android: Id = "@ + ID/readeventbutton" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "get a event" <br/> <button <br/> Android: Id = "@ + ID/writeeventbutton" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "input a event" <br/> </linearlayout> <br/>
Step 3: Modify the main core program calendardemo. java. The Code is as follows:
Package COM. tutor. calendardemo; </P> <p> Import Java. util. calendar; </P> <p> Import android. app. activity; <br/> Import android. content. contentvalues; <br/> Import android. database. cursor; <br/> Import android.net. uri; <br/> Import android. OS. build; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. button; <br/> Import androi D. widget. toast; </P> <p> public class calendardemo extends activity implements onclicklistener {<br/> private button mreaduserbutton; <br/> private button mreadeventbutton; <br/> private button mwriteeventbutton; </P> <p> Private Static string calandreurl = ""; <br/> Private Static string calandreeventurl = ""; <br/> Private Static string calandreremiderurl = ""; <br/> // to be compatible with calendars of different versions, the URL after 2.2 is changed. <br/> stati C {<br/> If (integer. parseint (build. version. SDK) >=8) {<br/> calandreurl = "content: // COM. android. calendar/calendars "; <br/> calandreeventurl =" content: // COM. android. calendar/events "; <br/> calandreremiderurl =" content: // COM. android. calendar/reminders "; </P> <p >}else {<br/> calandreurl =" content: // calendar/calendars "; <br/> calandreeventurl =" content: // calendar/events "; <br/> calandreremiderurl =" cont Ent: // calendar/reminders "; <br/>}< br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> setupviews (); <br/>}</P> <p> private void setupviews () {<br/> mreaduserbutton = (button) findviewbyid (R. id. readuserbutton); <br/> mreadeventbutton = (button) findviewbyid (R. id. readeventbutton); <br/> MWR Iteeventbutton = (button) findviewbyid (R. id. writeeventbutton); <br/> mreaduserbutton. setonclicklistener (this); <br/> mreadeventbutton. setonclicklistener (this); <br/> mwriteeventbutton. setonclicklistener (this); <br/>}</P> <p> @ override <br/> Public void onclick (view V) {<br/> If (V = mreaduserbutton) {</P> <p> cursor usercursor = getcontentresolver (). query (URI. parse (calandreurl), null, <br/> null, null, n Ull); <br/> If (usercursor. getcount ()> 0) {<br/> usercursor. movetofirst (); <br/> string username = usercursor. getstring (usercursor. getcolumnindex ("name"); <br/> toast. maketext (calendardemo. this, username, toast. length_long ). show (); <br/>}< br/>} else if (V = mreadeventbutton) {<br/> cursor eventcursor = getcontentresolver (). query (URI. parse (calandreeventurl), null, <br/> null, null, null); <br/> If (eventc Ursor. getcount ()> 0) {<br/> eventcursor. movetolast (); <br/> string eventtitle = eventcursor. getstring (eventcursor. getcolumnindex ("title"); <br/> toast. maketext (calendardemo. this, eventtitle, toast. length_long ). show (); <br/>}< br/>} else if (V = mwriteeventbutton) {<br/> // obtain the ID of the Gmail account to access <br/> string Calid = ""; <br/> cursor usercursor = getcontentresolver (). query (URI. parse (calandreurl), null, <br/> nu Ll, null, null); <br/> If (usercursor. getcount ()> 0) {<br/> usercursor. movetofirst (); <br/> Calid = usercursor. getstring (usercursor. getcolumnindex ("_ id"); </P> <p >}< br/> contentvalues event = new contentvalues (); <br/> event. put ("title", "communicate with Miss Kong"); <br/> event. put ("Description", "Frankie is invited by the flight attendant. We will discuss the operation at Sheraton after this evening. LOL ~ "); <Br/> // insert hoohbood@gmail.com to this account <br/> event. put ("calendar_id", Calid); </P> <p> calendar mcalendar = calendar. getinstance (); <br/> mcalendar. set (calendar. hour_of_day, 10); <br/> long start = mcalendar. gettime (). gettime (); <br/> mcalendar. set (calendar. hour_of_day, 11); <br/> long end = mcalendar. gettime (). gettime (); </P> <p> event. put ("dtstart", start); <br/> event. put ("dtend", end); <br/> event. put (" Hasalarm ", 1); </P> <p> URI newevent = getcontentresolver (). insert (URI. parse (calandreeventurl), event); <br/> long id = long. parselong (newevent. getlastpathsegment (); <br/> contentvalues values = new contentvalues (); <br/> values. put ("event_id", ID); <br/> // reminder 10 minutes in advance <br/> values. put ("Minutes", 10); <br/> getcontentresolver (). insert (URI. parse (calandreremiderurl), values); <br/> toast. maketext (Calendardemo. This, "the insertion event is successful !!! ", Toast. length_long). Show (); <br/>}< br/>}
Step 4: Apply for permissions in androidmanifest. xml. The Code is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "com. tutor. calendardemo "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name"> <br/> <activity Android: Name = ". calendardemo "<br/> Android: Label =" @ string/app_name "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> <br/> </Application> <br/> <uses-SDK Android: minsdkversion = "7"/> <br/> <uses-Permission Android: Name = "android. permission. read_calendar "/> <br/> <uses-Permission Android: Name =" android. permission. write_calendar "/> <br/> </manifest>
Step 5: run the android project and check the effect:
Run the first interface to get the login account name
Get event title insert an event
View one more event in the calendar. view event details.
OK. Let's talk about this today ~
Source code: http://download.csdn.net/source/3004309