Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 獲取與修改android自帶日歷日程

獲取與修改android自帶日歷日程

編輯:關於Android編程

Android中的日歷,只有真機才有,模擬上是沒有的,所以測試環境一定要真機!!

因為日歷是系統自帶的,所以我們讀寫它一定要申請權限,也就是在AndroidManifest.xml加如下兩行代碼(一個讀一個寫):


    
    
日歷日程URL在2.2以後版本有所改變

// 為了兼容不同版本的日歷,2.2以後url發生改變
	private static String calanderEventURL = "";
	static {
		if (Integer.parseInt(Build.VERSION.SDK) >= 8) {
			calanderEventURL = "content://com.android.calendar/events";
		} else {
			calanderEventURL = "content://calendar/events";
		}

	}
下面是獲取日程的主要代碼:
ContentResolver cr = context.getContentResolver();
		// 日歷裡面相應的Event的URI
		Uri uri = 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);
		}
下面是寫入日程的主要代碼
\

寫入的時候這幾個字段是必須的所以我就只寫了這幾個字段了

ContentResolver cr = context.getContentResolver();
		// 日歷裡面相應的Event的URI
		Uri uri = 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);
		}



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved