大家好,好久沒有更新blog了,今天給大家分享一下Android中一些自帶日歷的操作方法,這里主要用到了ContentProiver的知識.如果大家不明白ContentProvider建議先查一下資料,知道它是干什么的。這樣更容易下面的例子.
好了廢話不說,這里提個醒,Android中的日歷,**只有真機才有,模擬上是沒有的,所以測試環境一定要真機!!**
因為日歷是系統自帶的,所以我們讀寫它一定要申請權限,也就是在AndroidManifest.xml加如下兩行代碼(一個讀一個寫):
~~~
<uses-permission android:name="android.permission.READ_CALENDAR"/> <uses-permission android:name="android.permission.WRITE_CALENDAR"/>
~~~
Android中日歷用了三個URL,分別是日歷用戶的URL,事件的URL,事件提醒URL,三個URL在Android2.1之前是如下的樣子:
~~~
calanderURL = "content://calendar/calendars";calanderEventURL = "content://calendar/events";calanderRemiderURL= "content://calendar/reminders";
~~~
但是在Android2.2版本以后,三個URL有了改變,變成如下的樣子:
~~~
calanderURL = "content://com.android.calendar/calendars";calanderEventURL = "content://com.android.calendar/events";calanderRemiderURL = "content://com.android.calendar/reminders";??
~~~
還是老樣子,為了讓大家更好的理解,我寫了一個簡單的Demo,大家按照我的步驟一步一步的來:
第一步:新建一個Android工程命名為CalendarDemo.
第二步:修改main.xml布局文件,增加了三個按鈕,代碼如下:
~~~
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/readUserButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Get a User" /> <Button android:id="@+id/readEventButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Get a Event" /> <Button android:id="@+id/writeEventButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Input a Event" /></LinearLayout>
~~~
第三步:修改主核心程序CalendarDemo.java,代碼如下:
~~~
package com.tutor.calendardemo;import java.util.Calendar;import android.app.Activity;import android.content.ContentValues;import android.database.Cursor;import android.net.Uri;import android.os.Build;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class CalendarDemo extends Activity implements OnClickListener { private Button mReadUserButton; private Button mReadEventButton; private Button mWriteEventButton; private static String calanderURL = ""; private static String calanderEventURL = ""; private static String calanderRemiderURL = ""; //為了兼容不同版本的日歷,2.2以后url發生改變 static{ if(Integer.parseInt(Build.VERSION.SDK) >= 8){ calanderURL = "content://com.android.calendar/calendars"; calanderEventURL = "content://com.android.calendar/events"; calanderRemiderURL = "content://com.android.calendar/reminders"; }else{ calanderURL = "content://calendar/calendars"; calanderEventURL = "content://calendar/events"; calanderRemiderURL = "content://calendar/reminders"; } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setupViews(); } private void setupViews(){ mReadUserButton = (Button)findViewById(R.id.readUserButton); mReadEventButton = (Button)findViewById(R.id.readEventButton); mWriteEventButton = (Button)findViewById(R.id.writeEventButton); mReadUserButton.setOnClickListener(this); mReadEventButton.setOnClickListener(this); mWriteEventButton.setOnClickListener(this); } @Override public void onClick(View v) { if(v == mReadUserButton){ Cursor userCursor = getContentResolver().query(Uri.parse(calanderURL), null, null, null, null); if(userCursor.getCount() > 0){ userCursor.moveToFirst(); String userName = userCursor.getString(userCursor.getColumnIndex("name")); Toast.makeText(CalendarDemo.this, userName, Toast.LENGTH_LONG).show(); } }else if(v == mReadEventButton){ Cursor eventCursor = getContentResolver().query(Uri.parse(calanderEventURL), null, null, null, null); if(eventCursor.getCount() > 0){ eventCursor.moveToLast(); String eventTitle = eventCursor.getString(eventCursor.getColumnIndex("title")); Toast.makeText(CalendarDemo.this, eventTitle, Toast.LENGTH_LONG).show(); } }else if(v == mWriteEventButton){ //獲取要出入的gmail賬戶的id String calId = ""; Cursor userCursor = getContentResolver().query(Uri.parse(calanderURL), null, null, null, null); if(userCursor.getCount() > 0){ userCursor.moveToFirst(); calId = userCursor.getString(userCursor.getColumnIndex("_id")); } ContentValues event = new ContentValues(); event.put("title", "與蒼井空小姐動作交流"); event.put("description", "Frankie受空姐邀請,今天晚上10點以后將在Sheraton動作交流.lol~"); //插入hoohbood@gmail.com這個賬戶 event.put("calendar_id",calId); Calendar mCalendar = Calendar.getInstance(); mCalendar.set(Calendar.HOUR_OF_DAY,10); long start = mCalendar.getTime().getTime(); mCalendar.set(Calendar.HOUR_OF_DAY,11); long end = mCalendar.getTime().getTime(); event.put("dtstart", start); event.put("dtend", end); event.put("hasAlarm",1); Uri newEvent = getContentResolver().insert(Uri.parse(calanderEventURL), event); long id = Long.parseLong( newEvent.getLastPathSegment() ); ContentValues values = new ContentValues(); values.put( "event_id", id ); //提前10分鐘有提醒 values.put( "minutes", 10 ); getContentResolver().insert(Uri.parse(calanderRemiderURL), values); Toast.makeText(CalendarDemo.this, "插入事件成功!!!", Toast.LENGTH_LONG).show(); } }}??
~~~
第四步:在AndroidManifest.xml中申請權限,代碼如下:
~~~
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tutor.calendardemo" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".CalendarDemo" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="7" /> <uses-permission android:name="android.permission.READ_CALENDAR"/> <uses-permission android:name="android.permission.WRITE_CALENDAR"/> </manifest>
~~~
第五步:運行上述Android工程,查看效果:


運行首界面獲取登錄賬戶名

?
獲取事件title 插入一個事件

? 
查看日歷多了一條事件 查看事件詳情
?
Ok今天就講到這里,呵呵~
源代碼下載地址:[http://download.csdn.net/source/3004309](http://download.csdn.net/source/3004309)
- 前言
- (一)Android常用名令集錦(圖文并茂)!
- (二)Android Launcher抽屜類SlidingDrawer的使用!
- (三)Android 中自定義View的應用.
- (四)Android 中自定義屬性(attr.xml,TypedArray)的使用!
- (五)Android 中LayoutInflater的使用!
- (六)Android 中MenuInflater的使用(布局定義菜單)!
- (七)Android 中Preferences的使用!
- (八)Android Widget開發案例(世界杯倒計時!)
- (九)Android Handler的使用!!!
- (十)Android PopupWindow的使用!!!
- (十一)Android 通用獲取Ip的方法(判斷手機是否聯網的方法)!!!
- (十二)Android 在一個應用中如何啟動另外一個已安裝的應用!!!
- (十三)Android 數據庫SQLiteDatabase的使用!!
- (十四)Android Location的使用!!
- (十五)通過Location獲取Address的使用!
- (十六)Android中萬能的BaseAdapter(Spinner,ListView,GridView)的使用!
- Android 中的拿來主義(編譯,反編譯,AXMLPrinter2,smali,baksmali)!
- (十七)Android中Intent傳遞對象的兩種方法(Serializable,Parcelable)!
- (十八)列出Android設備中所有啟動的服務,及判斷某個服務是否開啟!
- (十九)Android開發中,使用線程應該注意的問題!
- (二十)Android與JavaScript方法相互調用!
- (二十一)Android中創建與幾種解析xml的方法!
- (二十二)Android中幾種圖像特效處理的集錦!!
- (二十三)Android中的日歷讀寫操作!!!
- (二十四)Android WebView的緩存!!!
- (二十五)Android 中的AIDL!!!