Looked for a day and a half, finally a little bit about. The following is your own understanding, the wrong place to look.
Android has a calendar function, the application can be based on a number of interfaces to develop their own functions, even the Calendar app is based on these interfaces developed, so we can use the program to write events to the system calendar, and then use the Calendar software on the phone to see the events we added. Online This information is also a lot, there are demo, but I did not find a can be run correctly, some lack of parameters, some version of the reason, more than 4.0 of the system this aspect of the change is relatively large, so can only check the data while modifying.
The general idea is to see if the system calendar has an account, and if you don't have to add one, then you can add events. There are a lot of parameters that must be filled, and what sync adapter. The save of the event is per account.
is not perfect at present, will delete all the accounts in the system calendar is deleted, please note!!!
The general framework uses: http://blog.csdn.net/Android_Tutor/article/details/6165470, thanks to the original author's sharing. In order to reflect the original author's yearning feelings, the event content remains unchanged!!
Based on this, the paper makes a complete and main comment.
User rights:
Android:name<= "Android.permission.READ_CALENDAR"/> . < android:name= "Android.permission.WRITE_CALENDAR"/>
Xml:
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical" > <TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Add Event to System Calendar" /> <ButtonAndroid:id= "@+id/inputaccount"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:onclick= "OnClick"Android:text= "Add Account" /> <ButtonAndroid:id= "@+id/readuserbutton"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:onclick= "OnClick"Android:text= "View Account" /> <ButtonAndroid:id= "@+id/readeventbutton"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:onclick= "OnClick"Android:text= "View Events" /> <ButtonAndroid:id= "@+id/writeeventbutton"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:onclick= "OnClick"Android:text= "Insert Event" /> <ButtonAndroid:id= "@+id/deleventbutton"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:onclick= "OnClick"Android:text= "Delete Event" /></LinearLayout>
Main program code:
PackageCom.example.canlendertesttwo;ImportJava.util.Calendar;ImportJava.util.TimeZone;Importandroid.app.Activity;Importandroid.content.ContentValues;ImportAndroid.database.Cursor;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;Importandroid.provider.CalendarContract;ImportAndroid.provider.CalendarContract.Calendars;Importandroid.provider.CalendarContract.Events;ImportAndroid.view.View;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {//after the Android2.2 version of the URL, the previous will not write Private StaticString Calanderurl = "Content://com.android.calendar/calendars"; Private StaticString Calandereventurl = "Content://com.android.calendar/events"; Private StaticString Calanderremiderurl = "Content://com.android.calendar/reminders"; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); } Public voidOnClick (View v) {if(V.getid () = = R.id.readuserbutton) {//Read System calendar account, if 0, add firstCursor usercursor = Getcontentresolver (). Query (Uri.parse (Calanderurl),NULL,NULL,NULL,NULL); System.out.println ("Count:" +Usercursor.getcount ()); Toast.maketext ( This, "Count:" +Usercursor.getcount (), Toast.length_long). Show (); for(Usercursor.movetofirst ();!usercursor.isafterlast (); Usercursor.movetonext ()) {System.out.println ("Name:" + usercursor.getstring (Usercursor.getcolumnindex ("account_name"))); String userName1= Usercursor.getstring (Usercursor.getcolumnindex ("name")); String USERNAME0= Usercursor.getstring (Usercursor.getcolumnindex ("account_name")); Toast.maketext ( This, "NAME:" + userName1 + "--account_name:" +USERNAME0, Toast.length_long). Show (); } } Else if(V.getid () = = R.id.inputaccount) {//Add a Calendar accountInitcalendars (); } Else if(V.getid () = = R.id.deleventbutton) {//Delete Event intRowNum = Getcontentresolver (). Delete (Uri.parse (Calanderurl), "_id!=-1",NULL);//Note: All accounts will be deleted, the newly added accounts usually start from id=1, //You can get _id= to add an account ID to delete the account you added.Toast.maketext (mainactivity. This, "Deleted:" +rownum, Toast.length_long). Show (); } Else if(V.getid () = = R.id.readeventbutton) {//Read EventsCursor eventcursor = Getcontentresolver (). Query (Uri.parse (Calandereventurl),NULL,NULL,NULL,NULL); if(Eventcursor.getcount () > 0) {eventcursor.movetolast (); //Note: This corresponds to the account where the event was added, and is added to the last accountString EventTitle = eventcursor.getstring (Eventcursor.getcolumnindex ("title")); Toast.maketext (mainactivity. This, EventTitle, Toast.length_long). Show (); } } Else if(V.getid () = =R.id.writeeventbutton) {//get the ID of the Gmail account you want to accessString Calid = ""; Cursor Usercursor= Getcontentresolver (). Query (Uri.parse (Calanderurl),NULL,NULL,NULL,NULL); if(Usercursor.getcount () > 0) {usercursor.movetolast (); //Note: To add to the last account, the developer can change the account to add the event as neededCalid = usercursor.getstring (Usercursor.getcolumnindex ("_id")); } Else{Toast.maketext ( This, "No account, please add account first", 0). Show (); return; } contentvalues Event=Newcontentvalues (); Event.put ("title", "Interaction with Miss Cang Aoi"); Event.put ("description", "Frankie by the stewardess invited, this evening after 10 o'clock will be in Sheraton Action Exchange. lol~"); //Insert AccountEvent.put ("calendar_id", Calid); System.out.println ("Calid:" +calid); Event.put ("Eventlocation", "Earth-Huaxia"); Calendar Mcalendar=calendar.getinstance (); Mcalendar.set (Calendar.hour_of_day,11); Mcalendar.set (Calendar.minute,45); LongStart =mcalendar.gettime (). GetTime (); Mcalendar.set (Calendar.hour_of_day,12); LongEnd =mcalendar.gettime (). GetTime (); Event.put ("Dtstart", start); Event.put ("Dtend", end); Event.put ("Hasalarm", 1); Event.put (Events.event_timezone,"Asia/shanghai");//This is the time zone, must have,//Add EventUri newevent =getcontentresolver (). Insert (Uri.parse (Calandereventurl), event); //settings for event reminders LongID =Long.parselong (Newevent.getlastpathsegment ()); Contentvalues Values=Newcontentvalues (); Values.put ("EVENT_ID", id); //10 minutes Early warningValues.put ("Minutes", 10); Getcontentresolver (). Insert (Uri.parse (calanderremiderurl), values); Toast.maketext (mainactivity. This, "Insert event successfully!!!", Toast.length_long). Show (); } } //Add an account Private voidInitcalendars () {TimeZone TimeZone=Timezone.getdefault (); Contentvalues value=Newcontentvalues (); Value.put (Calendars.name,"YY"); Value.put (Calendars.account_name,"[Email protected]"); Value.put (Calendars.account_type,"Com.android.exchange"); Value.put (Calendars.calendar_display_name,"Mytt"); Value.put (calendars.visible,1); Value.put (Calendars.calendar_color,-9206951); Value.put (Calendars.calendar_access_level, Calendars.cal_access_owner); Value.put (Calendars.sync_events,1); Value.put (Calendars.calendar_time_zone, Timezone.getid ()); Value.put (Calendars.owner_account,"[Email protected]"); Value.put (Calendars.can_organizer_respond,0); Uri Calendaruri=Calendars.content_uri; Calendaruri=calendaruri.buildupon (). Appendqueryparameter (Calendarcontract.caller_is_syncadapter,"True"). Appendqueryparameter (Calendars.account_name,"[Email protected]"). Appendqueryparameter (Calendars.account_type,"Com.android.exchange"). Build (); Getcontentresolver (). Insert (Calendaruri, value); } }
Operation Result:
Android adds an event to the system calendar