Android日历内容提供商:allDay为true但秒,分,小时不为0

时间:2013-12-03 08:55:39

标签: android android-contentprovider android-calendar

我正在查询内容提供商的约会。

    Uri.Builder builder = CalendarContract.Instances.CONTENT_URI.buildUpon();
    ContentUris.appendId(builder, startMillis);
    ContentUris.appendId(builder, endMillis);
    Uri uri = builder.build();

    String[] event_projection= new String[] {
        Instances.TITLE,               
        Instances.ALL_DAY,              
        Instances.CALENDAR_COLOR,       
        Instances.EVENT_ID,            
        Instances.BEGIN,               
        Instances.END,                 
        };

    String selection = CalendarContract.Instances.VISIBLE + "='1'";

    String sortBy = CalendarContract.Instances.BEGIN + " ASC, " +
                    CalendarContract.Instances.TITLE + " ASC";

    Cursor mCursor = cr.query(uri, event_projection, selection, null, sortBy);

有些用户发送以下崩溃报告:

java.lang.IllegalArgumentException: allDay is true but sec, min, hour are not 0.
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:167)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
    at android.content.ContentProviderProxy.query(ContentProviderNative.java:384)
    at android.content.ContentResolver.query(ContentResolver.java:372)
    at android.content.ContentResolver.query(ContentResolver.java:315)

这主要发生在Sony Xperia设备上,但也发生在Google Nexus10上。 查询数据时如何才能获得此异常? 首先插入数据时是否应该避免这种情况?

这是否意味着我必须使用try / catch包围查询? 这不会减慢查询速度吗?

2 个答案:

答案 0 :(得分:4)

是的,它确实听起来像数据被严重插入。如果它快速查询查询处理,则异常可以实际加快查询速度,但不会返回您要查找的数据。

异常似乎来自Time.compare http://developer.android.com/reference/android/text/format/Time.html#compare(android.text.format.Time,%20android.text.format.Time)

这可能是由查询的sortBy参数引起的。尝试使用null,看看它是否停止使用比较器。如果可行,那么当ALL_DAY为真时,它与您的数据集上的BEGIN排​​序不兼容。

您可以在从光标读取行并更新内存中的值以使比较器满意或使用您自己的比较器之后对系列进行排序,或者您可以修复数据(将sec,min,hour设置为0在运行查询之前,ALL_DAY为真。

也许一个好的做法是在异常和重试时修复数据。

答案 1 :(得分:1)

如果allDay设置为1,则eventTimezone必须为TIMEZONE_UTC且时间必须对应午夜边界,则表示小时,分钟,秒应为日历对象的零。

参考以下链接..

http://developer.android.com/reference/android/provider/CalendarContract.Events.html

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));           
cal.set(2014, 03, 18, 0, 0,0);