2011-05-10 75 views
0

我想日曆事件更新在Android 2.2設備(在添加新的事件或現有的事件被刪除)日曆事件更新? 我很初學者所以告訴我,這一步一步的解決方案..如何讓Android設備

回答

2

該代碼使用此代碼我可以知道哪些事件是更新可能會幫助你:)

public class myCalendar 
{ 

static Cursor cursor; 

public static void readCalendar(Context context) { 

    ContentResolver contentResolver = context.getContentResolver(); 

    // Fetch a list of all calendars synced with the device, their display names and whether the 
    // user has them selected for display. 


    cursor = contentResolver.query(Uri.parse("content://com.android.calendar/calendars"), 
       (new String[] { "_id", "displayName", "selected"}), null, null, null); 


    HashSet<String> calendarIds = new HashSet<String>(); 

    try 
    { 
     System.out.println("Count="+cursor.getCount()); 
     if(cursor.getCount() > 0) 
     { 
      System.out.println("the control is just inside of the cursor.count loop"); 
     while (cursor.moveToNext()) { 

      String _id = cursor.getString(0); 
      String displayName = cursor.getString(1); 
      Boolean selected = !cursor.getString(2).equals("0"); 

      System.out.println("Id: " + _id + " Display Name: " + displayName + " Selected: " + selected); 
      calendarIds.add(_id); 
     } 
    } 
    } 
    catch(AssertionError ex) 
    { 
     ex.printStackTrace(); 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 


    // For each calendar, display all the events from the previous week to the end of next week.   
    for (String id : calendarIds) { 
     Uri.Builder builder = Uri.parse("content://com.android.calendar/instances/when").buildUpon(); 
     //Uri.Builder builder = Uri.parse("content://com.android.calendar/calendars").buildUpon(); 
     long now = new Date().getTime(); 

     ContentUris.appendId(builder, now - DateUtils.DAY_IN_MILLIS * 10000); 
     ContentUris.appendId(builder, now + DateUtils.DAY_IN_MILLIS * 10000); 

     Cursor eventCursor = contentResolver.query(builder.build(), 
       new String[] { "title", "begin", "end", "allDay"}, "Calendars._id=" + 1, 
       null, "startDay ASC, startMinute ASC"); 

     System.out.println("eventCursor count="+eventCursor.getCount()); 
     if(eventCursor.getCount()>0) 
     { 

      eventCursor.moveToFirst(); 

      while (eventCursor.moveToNext()) 
      { 
       Object beg_date,beg_time,end_date,end_time; 

       final String title = eventCursor.getString(0); 
       final Date begin = new Date(eventCursor.getLong(1)); 
       final Date end = new Date(eventCursor.getLong(2)); 
       final Boolean allDay = !eventCursor.getString(3).equals("0"); 

     /* System.out.println("Title: " + title + " Begin: " + begin + " End: " + end + 
        " All Day: " + allDay); 
     */ 
       System.out.println("Title:"+title); 
       System.out.println("Begin:"+begin); 
       System.out.println("End:"+end); 
       System.out.println("All Day:"+allDay); 

       System.out.println("only date begin of events="+begin.getDate()); 
       System.out.println("only begin time of events="+begin.getHours() + ":" +begin.getMinutes() + ":" +begin.getSeconds()); 

       System.out.println("only date begin of events="+end.getDate()); 
       System.out.println("only begin time of events="+end.getHours() + ":" +end.getMinutes() + ":" +end.getSeconds()); 

       beg_date = begin.getDate(); 
       beg_time = begin.getHours()+":"+begin.getMinutes(); 

       end_date = end.getDate(); 
       end_time = end.getHours()+":"+end.getMinutes(); 





      } 
     } 
     break; 
    } 
} 


} 
+0

和Android設備中刪除.. – 2011-05-10 06:13:58

+0

你可以對它進行進一步的修改。目前它只是從谷歌日曆提取事件:) – 2011-05-10 06:26:53

+1

這是不支持android果凍豆請給我指導線或內容uri名稱。 – 2012-11-03 13:47:21