2012-03-06 114 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