1

我在从 Android 读取日历事件时遇到关于事件问题的开始和结束字段的问题。

代码:

public class MyActivity extends Activity {
/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final MyActivity self = this;
    Button button = (Button) this.findViewById(R.id.ReadButton);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ContentResolver contentResolver = self.getContentResolver();

            Uri uri = Uri.parse("content://com.android.calendar/instances/when");

            Uri.Builder builder = uri.buildUpon();

            //Range filter
            Calendar from = Calendar.getInstance();
            from.setTime(new Date());
            from.set(Calendar.HOUR,0);
            from.set(Calendar.MINUTE,0);
            from.set(Calendar.SECOND,0);
            from.set(Calendar.MILLISECOND, 0);

            Calendar to = Calendar.getInstance();
            to.setTime(from.getTime());
            to.set(Calendar.HOUR,11);
            to.set(Calendar.MINUTE,59);
            to.set(Calendar.SECOND,59);
            to.set(Calendar.MILLISECOND, 997);

            ContentUris.appendId(builder, from.getTime().getTime());
            ContentUris.appendId(builder, to.getTime().getTime());

            Cursor cursor = contentResolver.query(builder.build(), new String[]  {"_id", "title", "begin", "end", "allDay"}, "calendar_id=1", null, "startDay ASC, startMinute ASC");

            if(cursor.getCount()>0) {
                while (cursor.moveToNext()) {
                    final String title = cursor.getString(1);
                    final Date begin =new Date(cursor.getLong(2));
                    final Date end = new Date(cursor.getLong(3));

                    Log.i("ReadCalendar", title);
                    Log.i("ReadCalendar", begin.toString());
                    Log.i("ReadCalendar", end.toString());
                }
            }
        }
    });

}
}

我在谷歌日历中创建了一个活动,以下是详细信息
日期:2013 年 7 月 1 日
标题:香港特别行政区成立日
全天活动:选中

当我单击一个按钮时,流动是日志窗口的结果

07-01 16:52:06.491: INFO/ReadCalendar(1357): Hong Kong Special Administrative Region Establishment Day<br/>
07-01 16:52:06.491: INFO/ReadCalendar(1357): **Mon Jul 01 08:00:00 GMT+08:00 2013**<br/>
07-01 16:52:06.501: INFO/ReadCalendar(1357): **Tue Jul 02 08:00:00 GMT+08:00 2013**<br/>

问题是为什么我得到的开始和结束日是错误的。应该是 2013 年 7 月 1 日 00:00:00 到 2013 年 7 月 1 日 23-59:59

但是日历中的这个事件显示是正确的。

请帮忙!!!!

4

0 回答 0