2011-12-21 38 views
4

當我在Nexus S上開發API爲7的APP時,在我的日曆上創建新事件沒有任何問題。Android 4.0中日曆的位置是?

我使用的代碼讓我的手機上的日曆的位置:

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

的問題來了,當我更新了我的歌Nexus S到Android ICS 4.0。沒有改變任何代碼,我得到了一個錯誤。 在logcat的能讀:

  • 沒有這樣的柱:顯示名稱, 分貝= /數據/數據/ com.android.providers.calendar /數據庫/ calendar.db

當然光標被空值。 也許日曆數據庫中有任何更改? 所以,我想知道我怎麼可以創建新的日曆事件發展上的是Android 4.0

感謝的API 7應用程序;)

回答

3

日曆提供商是新的Android 4.0,所以你可能要檢討文檔:

http://developer.android.com/guide/topics/providers/calendar-provider.html

對於之前的Froyo版本,我知道曆法位於content://calendar/calendars,但它從此改變。

+0

固定,你說得對。謝謝;)我要寫我的代碼添加一個事件到一個特定的日曆。事實是,日曆表中的列名已更改。 – ElChencho

+0

很高興能盡我所能幫助你。 –

5

在Android 4.0中,日曆與Android 2.3中的Uri相同。所以我附上我的代碼以防其他人遇到同樣的問題。

public void addToCalendar(Context ctx, final String title, final String comment, final long dtstart, final long dtend) { 


    final ContentResolver cr = ctx.getContentResolver(); 
    Cursor cursor ; 

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

    /*if (Integer.parseInt(Build.VERSION.SDK) == 8) 
     cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null); 
    */ 

    //Get all the calendar ids and name available in the phone 
    if (cursor.moveToFirst()) { 
     final String[] calNames = new String[cursor.getCount()]; 
     final int[] calIds = new int[cursor.getCount()]; 
     for (int i = 0; i < calNames.length; i++) { 
      calIds[i] = cursor.getInt(0); 
      calNames[i] = cursor.getString(1); 
      cursor.moveToNext(); 
     } 

     //Creation of a new event in calendar whose position is 0 on the phone 
     ContentValues cv = new ContentValues(); 
     cv.put("calendar_id", calIds[0]); 
     cv.put("title", title); 
     cv.put("dtstart", dtstart); 
     cv.put("dtend", dtend); 
     cv.put("eventTimezone","Spain"); 
     cv.put("description", comment); 

     //Insertion on the events of the calendar 
     cr.insert(Uri.parse("content://com.android.calendar/events"), cv); 

     /*Uri newEvent ; 
     if (Integer.parseInt(Build.VERSION.SDK) == 8) 
      newEvent = cr.insert(Uri.parse("content://com.android.calendar/events"), cv); 
     */ 

     finish(); 

    } 
    cursor.close(); 

} 

您有以下的所有信息:http://developer.android.com/guide/topics/providers/calendar-provider.html約翰說過。