2012-03-06 39 views
1

作爲項目的一部分,我創建了一個Android應用程序與在線數據庫(MySQL的)的任命整合通信採取了在線日曆。 我收集數據,轉換成JSON,但是當Android手機我遇到probleme的議程inscrires,這裏是我的代碼:(對不起,我的英語)寫在android日曆

編輯:

public class calendrier extends Activity { 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     Cursor cursor = getContentResolver() 
       .query(Uri.parse("content://com.android.calendar/calendars"), 
         new String[] { "_id", "displayName" }, "selected=1", 
         null, null); 
     if (cursor != null && cursor.moveToFirst()) { 
      String[] calNames = new String[cursor.getCount()]; 
      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(); 
      } 
      cursor.close(); 
      if (calIds.length > 0) { 
       // we're safe here to do any further work 
      } 

      // grab calendar id from above 
      int cal_id = calIds[0]; 

      // set the content value 
      ContentValues cv = new ContentValues(); 

      cv.put("calendar_id", cal_id); 
      cv.put("title", "titre"); 
      cv.put("description", "bla bla bla"); 
      cv.put("eventLocation", "city"); 
      // note: you're going to need to convert the desired date into 
      // milliseconds 
      cv.put("dtstart", System.currentTimeMillis()); 
      cv.put("dtend", System.currentTimeMillis() 
        + DateUtils.DAY_IN_MILLIS); 
      cv.put("allDay", 0); // true = 1, false = 0 
      cv.put("hasAlarm", 1); 

      // once desired fields are set, insert it into the table 
      getContentResolver().insert(
        Uri.parse("content://com.android.calendar/events"), cv); 
     } 
    } 
} 

這代碼工作,但它問我,如果我想參加這次活動,當我打開它,我希望他沒有做

感謝

+0

嘗試閱讀源代碼。 – JoxTraex 2012-03-06 08:06:14

回答

0

//沒有ü包括允許在manifest.xml文件

android.permission.READ_CALENDAR 
android.permission.WRITE_CALENDAR 

String WRITE_CALENDAR Allows an application to write (but not read) the user's calendar data. 
+0

是的,我包括這個 <使用權限android:name =「android.permission.READ_CALENDAR」/> <使用權限android:name =「android.permission.WRITE_CALENDAR」/> – user1250193 2012-03-06 08:20:28

0

日曆的URI已經從Android 2.2的改變。

Old (2.1 and before): content://calendar/ 
New (2.2): content://com.android.calendar/ 

改變你的Uri到新的。

有默認情況下在Android中沒有日曆應用。所以Uri可能根本不工作。 如果,你所得到的第一個錯誤消息「無法找到com.android.calendar供應商信息」,你需要檢查你使用的調試手機使用的日曆URI。 要在日曆事件中插入時間,可以使用以下代碼示例:

Calendar calendar = Calendar.getInstance(); 
    calendar.getTimeInMillis(); 
    cv.put("dtstart", ""+calendar.getTimeInMillis()); 
    cv.put("dtend", ""+calendar.getTimeInMillis()+10000); 
+0

謝謝但不知道的URL內容:/ /com.android.calendar/ :(:( – user1250193 2012-03-06 08:29:23

+0

感謝您的答覆,Android 2.3的需求使用:內容://com.android.calendar/calendars ,但我有新的錯誤:9月3日至6日:47:01.477 E/AndroidRuntime(17876):導致:java.lang.NullPointerException: – user1250193 2012-03-06 08:50:28

+0

使用LogCat跟蹤發佈錯誤 – Akhil 2012-03-06 09:16:01