2011-12-08 28 views
0

我需要能夠從我的Android應用程序在Google日曆中創建一個事件。我相信有一個日曆API,但我從來沒有使用它。我對Android開發相當陌生,所以我從早先的瀏覽中找到了一些示例,並使用以下代碼嘗試更新我的Android日曆。如何從我的Android代碼動態更新日曆

public static boolean updateCalendar(Context context,String cal_Id,String eventId) 
{ 
try{ 

    Uri CALENDAR_URI = Uri.parse(CAL_URI+"events"); 
    Cursor c = context.getContentResolver().query(CALENDAR_URI, null, null, null, null); 
    String[] s = c.getColumnNames(); 

    if (c.moveToFirst()) 
    { 
      while (c.moveToNext()) 
     { 

      String _id = c.getString(c.getColumnIndex("_id")); 
      String CalId = c.getString(c.getColumnIndex("calendar_id"));    
      if ((_id==null) && (CalId == null)) 
      { 
          return false; 
      } 
      else 
      { 
       if (_id.equals(eventId) && CalId.equals(cal_Id)) 
          { 
        Uri uri = ContentUris.withAppendedId(CALENDAR_URI, Integer.parseInt(_id)); 
        context.getContentResolver().update(uri, null, null, null);// need to give your data here 
        return true; 
       } 
      } 
     } 
    } 


} 
finally 
{ 
    return true; 
} 
} 

然而,當我運行它getColumnNames不會被調用,代碼跳轉直接到線context.getContentResolver()更新(URI,NULL,NULL,NULL);然後退出。

我把一對夫婦的測試賽在我的日曆,爲什麼這些代碼並沒有接他們?

回答

0

使用這種添加事件壓延到特定的日期和時間

Uri event1; 
    long epoch; 
    long epoch1; 


Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this) + "events"); 
       ContentResolver cr = getContentResolver(); 

       ContentValues values = new ContentValues(); 

       try 
       { 
        epoch = new java.text.SimpleDateFormat ("yyyy-MM-dd hh:mm").parse(YourStartDate+" "+YourStratTime).getTime(); 
        //epoch=epoch; 
        Log.e("epoch",String.valueOf(epoch)); 
        epoch1 = new java.text.SimpleDateFormat ("yyyy-MM-dd hh:mm").parse(YourStartDate+" "+YourEndDate).getTime(); 
        //epoch1=epoch1; 
        Log.e("epoch1",String.valueOf(epoch1)); 
       } catch (ParseException e) 
       { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

       values.put("calendar_id", 1); 
       values.put("title", "Appoitment"); 
       values.put("allDay", 0); 
       values.put("dtstart",epoch); // event starts at 11 minutes from now 
       values.put("dtend", epoch1); // ends 60 minutes from now 
       values.put("description", "Your consulting date and time "); 
       values.put("visibility", 0); 
       values.put("hasAlarm", 1); 
       if(EVENTS_URI!=null) 
       { 
       event1 = cr.insert(EVENTS_URI, values); 
       } 

       // reminder insert 
       Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this) + "reminders"); 
       values = new ContentValues(); 
       values.put("event_id", Long.parseLong(event1.getLastPathSegment())); 
       values.put("method", 1); 
       values.put("minutes", 10); 
       if(REMINDERS_URI!=null) 
       { 
       cr.insert(REMINDERS_URI, values); 
       } 

getCalendarUroBase功能:

private String getCalendarUriBase(Activity act) { 

       String calendarUriBase = null; 
       Uri calendars = Uri.parse("content://calendar/calendars"); 
       Cursor managedCursor = null; 
       try { 
        managedCursor = act.managedQuery(calendars, null, null, null, null); 
       } catch (Exception e) { 
       } 
       if (managedCursor != null) { 
        calendarUriBase = "content://calendar/"; 
       } else { 
        calendars = Uri.parse("content://com.android.calendar/calendars"); 
        try { 
         managedCursor = act.managedQuery(calendars, null, null, null, null); 
        } catch (Exception e) { 
        } 
        if (managedCursor != null) { 
         calendarUriBase = "content://com.android.calendar/"; 
        } 
       } 
       return calendarUriBase; 
      } 

注:由於每次採樣的日期應該是在YYYY-MM-DD,時間應在HH:MM格式

+0

感謝您的回覆,我已經嵌入代碼到我的應用程序,但我發現一對夫婦的錯誤消息。這兩行代碼Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this)+「events」); ContentResolver cr = getContentResolver();正在拋出錯誤消息,第一個說'你不能在靜態上下文中使用它',第二個說'不能從ContextWrapper類型對非靜態方法getContentResolver()進行靜態引用「。 – user616076

+0

可能已放置在靜態方法的代碼檢查一次,可能不會在模擬器 – Abhi

+0

http://stackoverflow.com/questions/4969171/cannot-make-a-static-reference-to-the-non-static-工作方法檢查這個 – Abhi