如何使用新的Android 4.0冰淇淋三明治API读取和编辑Android日历事件?

我们正试图向用户展示冰淇淋三明治日历视图,当他们想要添加一个新的事件。 我们只能在模拟器中testing这个。

另一个问题是,我们无法find任何示例如何使用CalendarProvider。 这是处理三明治日历的正确课程?

Google Gdata API会更容易吗?

[编辑]所以我们的工作是添加一个事件到日历,但它不是通过API,我们在正确的观点打开日历。 但现在的问题是,它不能在模拟器中工作,因为我们没有得到同步的日历。

所以问题是:如何阅读和可能编辑使用新的Android 4.0冰淇淋三明治API Android日历事件?

这里的代码可以让你直接添加一个事件:

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,所以以下是查询日历列表的方法:

 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权限才能工作。

如果您想避免必须请求权限,则还可以使用Intent代表日历应用程序以您的名义创build新事件:

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

您可以使用下面的代码来设置时区,它适用于我

 TimeZone timeZone = TimeZone.getDefault(); values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID()); 

确保你没有使用ICS和JellyBean设备的可见性(apiLevel> = 14)

尝试这个 –

 ContentValues values= new ContentValues(); int apiLevel = android.os.Build.VERSION.SDK_INT; if(apiLevel<14) values.put("visibility", 0); 

仅在设备版本低于14(ICS)时才使用可见性

我已经创build了以下方法来处理TIMEZONE的灵活性

//将TimeZone转换为GMT以保存本地Db以避免//通过服务器争论

 private String convertDateTimeZone(long originalDate) { String newDate = ""; Date date = new Date(originalDate); DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss"); Date parsed = null; try { parsed = formatter.parse(formatter.format(date).toString()); TimeZone tz = TimeZone.getTimeZone("GMT"); SimpleDateFormat destFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); destFormat.setTimeZone(tz); newDate = destFormat.format(parsed); } catch (Exception e) { } return newDate; } 

我使用下面的代码来阅读日历中的所有事件。

 public boolean isEventInCal(Context context, String cal_meeting_id) { Cursor cursor = context.getContentResolver().query( Uri.parse("content://com.android.calendar/events"), new String[] { "_id" }, " _id = ? ", new String[] { cal_meeting_id }, null); if (cursor.moveToFirst()) { // will give all events return true; } return false; } 
 public static ArrayList<String> readCalendarEvent(Context context) { Cursor cursor = context.getContentResolver() .query( Uri.parse("content://com.android.calendar/events"), new String[] { "calendar_id", "title", "description", "dtstart", "dtend", "eventLocation" }, null, null, null); cursor.moveToFirst(); // fetching calendars name String CNames[] = new String[cursor.getCount()]; // fetching calendars id nameOfEvent.clear(); startDates.clear(); endDates.clear(); descriptions.clear(); Log.d("cnameslength",""+CNames.length); if (CNames.length==0) { Toast.makeText(context,"No event exists in calendar",Toast.LENGTH_LONG).show(); } for (int i = 0; i < CNames.length; i++) { nameOfEvent.add(cursor.getString(1)); startDates.add(getDate(Long.parseLong(cursor.getString(3)))); endDates.add(getDate(Long.parseLong(cursor.getString(4)))); descriptions.add(cursor.getString(2)); CNames[i] = cursor.getString(1); cursor.moveToNext(); Log.d("datacur",""+nameOfEvent.get(i)); Log.d("datacur",""+startDates.get(i)); Log.d("datacur",""+endDates.get(i)); Log.d("datacur",""+descriptions.get(i)); String filename=nameOfEvent.get(i)+"::"+startDates.get(i)+"::"+endDates.get(i)+"::"+descriptions.get(i); generateNoteOnSD(context,nameOfEvent.get(i),filename); } return nameOfEvent; } public static void generateNoteOnSD(Context context, String sFileName, String sBody) { try { File root = new File(Environment.getExternalStorageDirectory(), "Notes"); if (!root.exists()) { root.mkdirs(); } File gpxfile = new File(root, sFileName); FileWriter writer = new FileWriter(gpxfile); writer.append(sBody); writer.flush(); writer.close(); Toast.makeText(context, "Successfully Backup Created", Toast.LENGTH_SHORT).show(); } catch (IOException e) { e.printStackTrace(); } }