如何阅读和使用全新的Andr​​oid 4.0冰淇淋三明治API修改Android的日历事件?明治、冰淇淋、日历、事件

2023-09-12 06:55:52 作者:节奏才是王道i

我们正在试图展示用户的冰淇淋三明治日历视图,当他们想增加一个新的事件。我们只能在模拟器进行测试。

We are trying to show user the Ice cream Sandwich calendar view, when they want to add a new event. We can only test this in emulator.

另外一个问题是,我们无法找到任何例子如何使用CalendarProvider。这是一个正确的类,当涉及到处理三明治日历?

The other problem is that we can't find any examples how to use CalendarProvider. This is the right class when it comes to dealing with Sandwich calendar?

这将是容易做与谷歌的GData API?

Would this be easier to do with Google Gdata API?

所以我们有的工作是添加事件日历,但它不是通过API,我们打开正确的视图的日历。但现在的问题是,它不会在模拟器上工作,因为我们一直无法得到日历同步。

So what we got working was adding an event to calendar but it was not through the API, we open the calendar in the correct view. But now the problem is that it does not work in the emulator because we haven't been able to get the calendar synced.

所以,问题是:如何阅读,并可能采用全新的Andr​​oid 4.0冰淇淋三明治API修改的Andr​​oid日历事件

So the question is: How to read and possibly edit Android calendar events using the new Android 4.0 Ice Cream Sandwich API?

推荐答案

下面是code,可以让你直接添加事件:

Here's code that will let you add an event directly:

import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.provider.CalendarContract;

import java.util.Calendar;

// Construct event details
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 9, 14, 7, 30);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 9, 14, 8, 45);
endMillis = endTime.getTimeInMillis();

// Insert Event
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
TimeZone timeZone = TimeZone.getDefault();
values.put(CalendarContract.Events.DTSTART, startMillis);
values.put(CalendarContract.Events.DTEND, endMillis);
values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
values.put(CalendarContract.Events.TITLE, "Walk The Dog");
values.put(CalendarContract.Events.DESCRIPTION, "My dog is bored, so we're going on a really long walk!");
values.put(CalendarContract.Events.CALENDAR_ID, 3);
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);

// Retrieve ID for new event
String eventID = uri.getLastPathSegment();

您将需要CALENDAR_ID,所以这里的如何查询日历列表:

You'll need the CALENDAR_ID, so here's how to query the list of calendars:

Uri uri = CalendarContract.Calendars.CONTENT_URI;
String[] projection = new String[] {
       CalendarContract.Calendars._ID,
       CalendarContract.Calendars.ACCOUNT_NAME,
       CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
       CalendarContract.Calendars.NAME,
       CalendarContract.Calendars.CALENDAR_COLOR
};

Cursor calendarCursor = managedQuery(uri, projection, null, null, null);

您需要请 android.permission.READ_CALENDAR 许可,所有这一切工作。

You'll need to request the android.permission.READ_CALENDAR permission for all of this to work.

如果你想避免不得不请求的权限,也可以要求日历应用程序通过创建代表你的新事件的意图

If you want to avoid having to request permissions, you can also ask the Calendar app to create a new event on your behalf by using an Intent:

Intent intent = new Intent(Intent.ACTION_INSERT)
         .setType("vnd.android.cursor.item/event")
         .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis())
         .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis())
         .putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY , false) // just included for completeness
         .putExtra(Events.TITLE, "My Awesome Event")
         .putExtra(Events.DESCRIPTION, "Heading out with friends to do something awesome.")
         .putExtra(Events.EVENT_LOCATION, "Earth")
         .putExtra(Events.RRULE, "FREQ=DAILY;COUNT=10") 
         .putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY)
         .putExtra(Events.ACCESS_LEVEL, Events.ACCESS_PRIVATE)
         .putExtra(Intent.EXTRA_EMAIL, "my.friend@example.com");
startActivity(intent);