大家好,好久沒有更新blog了,今天給大家分享一下Android中一些內建日曆的操作方法,這裡主要用到了ContentProiver的知識.如果大家不明白ContentProvider建議先查一下資料,知道它是幹什麼的。這樣更容易下面的例子.
好了廢話不說,這裡提個醒,Android中的日曆,只有真機才有,類比上是沒有的,所以測試環境一定要真機!!
因為日曆是系統內建的,所以我們讀寫它一定要申請許可權,也就是在AndroidManifest.xml加如下兩行代碼(一個讀一個寫):
<uses-permission android:name="android.permission.READ_CALENDAR"/><br /><uses-permission android:name="android.permission.WRITE_CALENDAR"/>
Android中日曆用了三個URL,分別是日曆使用者的URL,事件的URL,事件提醒URL,三個URL在Android2.1之前是如下的樣子:
calanderURL = "content://calendar/calendars";<br />calanderEventURL = "content://calendar/events";<br />calanderRemiderURL= "content://calendar/reminders";
但是在Android2.2版本以後,三個URL有了改變,變成如下的樣子:
calanderURL = "content://com.android.calendar/calendars";<br />calanderEventURL = "content://com.android.calendar/events";<br />calanderRemiderURL = "content://com.android.calendar/reminders";
還是老樣子,為了讓大家更好的理解,我寫了一個簡單的Demo,大家按照我的步驟一步一步的來:
第一步:建立一個Android工程命名為CalendarDemo.
第二步:修改main.xml布局檔案,增加了三個按鈕,代碼如下:
<?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 /> ><br /> <TextView<br />android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br />android:text="@string/hello"<br /> /><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 /> /><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 /> /><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 /> /><br /></LinearLayout><br />
第三步:修改主核心程式CalendarDemo.java,代碼如下:
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 android.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 calanderURL = "";<br />private static String calanderEventURL = "";<br /> private static String calanderRemiderURL = "";<br /> //為了相容不同版本的日曆,2.2以後url發生改變<br />static{<br />if(Integer.parseInt(Build.VERSION.SDK) >= 8){<br />calanderURL = "content://com.android.calendar/calendars";<br />calanderEventURL = "content://com.android.calendar/events";<br />calanderRemiderURL = "content://com.android.calendar/reminders";</p><p>}else{<br />calanderURL = "content://calendar/calendars";<br />calanderEventURL = "content://calendar/events";<br />calanderRemiderURL = "content://calendar/reminders";<br />}<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 /> mWriteEventButton = (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(calanderURL), null,<br />null, null, null);<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(calanderEventURL), null,<br />null, null, null);<br />if(eventCursor.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 />//擷取要出入的gmail賬戶的id<br />String calId = "";<br />Cursor userCursor = getContentResolver().query(Uri.parse(calanderURL), null,<br />null, 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", "與蒼井空小姐動作交流");<br /> event.put("description", "Frankie受空姐邀請,今天晚上10點以後將在Sheraton動作交流.lol~");<br /> //插入hoohbood@gmail.com這個賬戶<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(calanderEventURL), event);<br /> long id = Long.parseLong( newEvent.getLastPathSegment() );<br /> ContentValues values = new ContentValues();<br /> values.put( "event_id", id );<br /> //提前10分鐘有提醒<br /> values.put( "minutes", 10 );<br /> getContentResolver().insert(Uri.parse(calanderRemiderURL), values);<br /> Toast.makeText(CalendarDemo.this, "插入事件成功!!!", Toast.LENGTH_LONG).show();<br />}<br />}<br />}
第四步:在AndroidManifest.xml中申請許可權,代碼如下:
<?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>
第五步:運行上述Android工程,查看效果:
運行首介面 擷取登入賬戶名
擷取事件title 插入一個事件
查看日曆多了一條事件 查看事件詳情
Ok今天就講到這裡,呵呵~
原始碼:http://download.csdn.net/source/3004309