본문 바로가기

About Programing/05. Android

Android 내장 Calendar에서 일정 읽어 오기

Google Calendar와 동기화 하는 방법을 찾다가 Java에서는 쉽게 구현 가능하지만,
Android와 Google Calendar API가 궁합이 맞지 않다는 것을 알고 우회하기로 하였다.

어차피 내장 Calendar는 Google Calendar와 자동으로 동기화가 되니,
내장 Calendar에서 값을 읽어 오기로 하였다.


// Main.java
package com.calendar;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Main extends ListActivity {
    /** Called when the activity is first created. */
	
	ArrayAdapter mArrayAdapter;
	List mItems = new ArrayList();
	ListView mListView;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mListView = (ListView)findViewById(android.R.id.list);
        
        setCalendar();
    }
    
    public void setCalendar ()
	{
		Uri calendars = Uri.parse("content://com.android.calendar/events");
		
		String[] projection = new String[] {
				"calendar_id",
				"htmlUri",
				"title",
				"eventLocation",
				"description",
				"eventStatus",
				"selfAttendeeStatus",
				"commentsUri",
				"dtstart",
				"dtend",
				"eventTimezone",
				"duration",
				"allDay",
				"visibility",
				"transparency",
				"hasAlarm",
				"hasExtendedProperties",
				"rrule",
				"rdate",
				"exrule",
				"exdate",
				"originalEvent",
				"originalInstanceTime",
				"originalAllDay",
				"lastDate",
				"hasAttendeeData",
				"guestsCanModify",
				"guestsCanInviteOthers",
				"guestsCanSeeGuests",
				"organizer",
				"deleted"
		} ;
		Cursor managedCursor = getContentResolver().query(calendars, projection, "selected=1", null, null) ;

		if(managedCursor.moveToFirst()) {
			int[] calendar_id               = new int[managedCursor.getCount()];
			
			String[] htmlUri                = new String[managedCursor.getCount()];
			String[] title                  = new String[managedCursor.getCount()];
			String[] eventLocation          = new String[managedCursor.getCount()];
			String[] description            = new String[managedCursor.getCount()];
			int[] eventStatus               = new int[managedCursor.getCount()];
			int[] selfAttendeeStatus        = new int[managedCursor.getCount()];
			String[] commentsUri            = new String[managedCursor.getCount()];
			int[] dtstart                = new int[managedCursor.getCount()];
			int[] dtend                  = new int[managedCursor.getCount()];
			String[] eventTimezone          = new String[managedCursor.getCount()];
			String[] duration               = new String[managedCursor.getCount()];
			int[] allDay                    = new int[managedCursor.getCount()];
			int[] visibility                = new int[managedCursor.getCount()];
			int[] transparency              = new int[managedCursor.getCount()];
			int[] hasAlarm                  = new int[managedCursor.getCount()];
			int[] hasExtendedProperties     = new int[managedCursor.getCount()];
			String[] rrule                  = new String[managedCursor.getCount()];
			String[] rdate                  = new String[managedCursor.getCount()];
			String[] exrule                 = new String[managedCursor.getCount()];
			String[] exdate                 = new String[managedCursor.getCount()];
			String[] originalEvent          = new String[managedCursor.getCount()];
			int[] originalInstanceTime      = new int[managedCursor.getCount()];
			int[] originalAllDay            = new int[managedCursor.getCount()];
			int[] lastDate               = new int[managedCursor.getCount()];
			int[] hasAttendeeData           = new int[managedCursor.getCount()];
			int[] guestsCanModify           = new int[managedCursor.getCount()];
			int[] guestsCanInviteOthers     = new int[managedCursor.getCount()];
			int[] guestsCanSeeGuests        = new int[managedCursor.getCount()];
			String[] organizer              = new String[managedCursor.getCount()];
			int[] deleted                   = new int[managedCursor.getCount()];
			
			for (int i = 0 ; i < title.length ; i++) {
				calendar_id[i] = managedCursor.getInt(0);
				Log.i("Calendar", "ID : " + calendar_id[i]);
				htmlUri[i] = managedCursor.getString(1);
				Log.i("Calendar", "htmlUri : " + htmlUri[i]);
				title[i] = managedCursor.getString(2);
				Log.i("Calendar", "title : " + title[i]);
				eventLocation[i] = managedCursor.getString(3);
				Log.i("Calendar", "eventLocation : " + eventLocation[i]);
				description[i] = managedCursor.getString(4);
				eventStatus[i] = managedCursor.getInt(5);
				selfAttendeeStatus[i] = managedCursor.getInt(6);
				commentsUri[i] = managedCursor.getString(7);
				dtstart[i] = managedCursor.getInt(8);
				dtend[i] = managedCursor.getInt(9);
				eventTimezone[i] = managedCursor.getString(10);
				duration[i] = managedCursor.getString(11);
				allDay[i] = managedCursor.getInt(12);
				visibility[i] = managedCursor.getInt(13);
				transparency[i] = managedCursor.getInt(14);
				hasAlarm[i] = managedCursor.getInt(15);
				hasExtendedProperties[i] = managedCursor.getInt(16);
				rrule[i] = managedCursor.getString(17);
				rdate[i] = managedCursor.getString(18);
				exrule[i] = managedCursor.getString(19);
				exdate[i] = managedCursor.getString(20);
				originalEvent[i] = managedCursor.getString(21);
				originalInstanceTime[i] = managedCursor.getInt(22);
				originalAllDay[i] = managedCursor.getInt(23);
				lastDate[i] = managedCursor.getInt(24);
				hasAttendeeData[i] = managedCursor.getInt(25);
				guestsCanModify[i] = managedCursor.getInt(26);
				guestsCanInviteOthers[i] = managedCursor.getInt(27);
				guestsCanSeeGuests[i] = managedCursor.getInt(28);
				organizer[i] = managedCursor.getString(29);
				deleted[i] = managedCursor.getInt(30);
				
				if (title[i] != null)
				{
					mItems.add(title[i]);
				}
				
				managedCursor.moveToNext() ;
			}
			managedCursor.close();
			
			mArrayAdapter = new ArrayAdapter (this, android.R.layout.simple_list_item_1, mItems);
			mListView = (ListView)findViewById (android.R.id.list);
			mListView.setAdapter(mArrayAdapter);
			mListView.setItemsCanFocus(true);
		}
	}
}

마지막줄에 </string></string></string></string>이 자꾸 붙네요-_-
무시하세요.

// main.xml




    	



LISTVIEW 와 LINEARLAYOUT 이 모두 대문자로 적혀있는데, 수정이 안되네요-_-;;
ListView, LinearLayout 으로 수정해서 사용하시기 바랍니다.

// AndroidManifest.xml