Android 4.0中日历的位置在哪里?

4
当我在Nexus S上开发一个API为7的APP时,在我的日历上创建新事件时没有问题。 我使用了以下代码来获取手机上日历的位置:
cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null);

问题出现在我将我的Nexus S 更新到Android ICS 4.0时。没有改变任何代码,我就遇到了一个错误。 在logcat中,我能看到以下信息:

  • no such column:displayname, db=/data/data/com.android.providers.calendar/databases/calendar.db

当然,游标是空的。 也许是日历数据库的任何更改? 所以,我想知道如何在Android 4.0上开发API 7应用程序来创建新的日历事件。

谢谢;)

2个回答

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();

}

你可以在John之前提到的http://developer.android.com/guide/topics/providers/calendar-provider.html网站上找到所有相关信息。

3

没错,你是对的。谢谢 ;) 我要写我的代码来添加一个事件到特定的日历中。事实是,日历表中的列名已经改变了。 - ElChencho

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接