android - 各种 API 上的日历

标签 android events calendar uri

我在向 Android 日历添加事件时遇到问题。 我的最低 SDK 版本是 7。我使用 Intent 添加事件,但各种 API 都存在问题。 我用这个:

             String eventUriString;
    if (Build.VERSION.SDK_INT > 7)
     eventUriString = "content://com.android.calendar/events";
    else  eventUriString = "content://calendar/events";

    ContentValues eventValues = new ContentValues();

    eventValues.put("calendar_id", 1); 
    eventValues.put("title", "title");
    eventValues.put("description", desc);
    eventValues.put("eventLocation", "");

    long endDate = startDate + 1000 * 60 * 60; 

    eventValues.put("dtstart", MyClass.getDate());
    eventValues.put("dtend", endDate);

    eventValues.put("eventStatus", 0); 
    eventValues.put("visibility", 2); 
    eventValues.put("transparency", 0); 

    eventValues.put("hasAlarm", 0); 

    Uri eventUri = context.getContentResolver().insert(Uri.parse(eventUriString), eventValues);
    long eventID = Long.parseLong(eventUri.getLastPathSegment());

编辑我使用的一些事件:

String calendarUriBase = null;
            long id = MyEvents.getID(p);  //something ID from my another class
        Uri calendars = Uri.parse("content://calendar/calendars");
        Cursor managedCursor = null;
        try {
            managedCursor = managedQuery(calendars, null, null, null, null);
        } catch (Exception e) {
            // eat
        }

        if (managedCursor != null) {
            calendarUriBase = "content://calendar/";
        } else {
            calendars = Uri.parse("content://com.android.calendar/calendars");
            try {
                managedCursor = managedQuery(calendars, null, null, null, null);
            } catch (Exception e) {
                // eat
            }

            if (managedCursor != null) {
                calendarUriBase = "content://com.android.calendar/";
            }

        }

        ContentValues event = new ContentValues();

        event.put("title", "new Title");

        Uri eventsUri = Uri.parse(calendarUriBase+"events");
        Uri eventUri = ContentUris.withAppendedId(eventsUri, id);

        getContentResolver().update(eventUri, event, null, null);

它适用于我的手机(SE X8 和 Android 2.3.7)但不适用于 SDK 14。

是否有通用代码,我可以使用它来向 Android 日历添加事件,适用于各种 SDK?我不知道它是怎么做到的。我必须使用 API 7,因为我的经理想要。你知道怎么做吗?

最佳答案

在 ICS 中引入了一个新的日历 API,因此您的代码将无法在 ICS 中运行。 New Public APIs in ICS

为了支持向所有日历添加事件,您可以像这样更改代码 -

if (Build.VERSION.SDK_INT >= 14) {
                saveCalendarEventICS();
            }
            else {
                int cal_id = getCalendar_ID();
                if(cal_id != 0){
                    saveCalendarEvent(cal_id);
                }
            }

private int getCalendar_ID() {
            // TODO Auto-generated method stub
            int calendar_id         = 0;
            String[] projection     = new String[] { "_id", "name" };
            String selection        = "selected=1";
            String path             = "calendars";
            Cursor calendarCursor   = getCalendarCursor(projection, selection, path);

            if (calendarCursor != null && calendarCursor.moveToFirst()) {
                int nameColumn      = calendarCursor.getColumnIndex("name");
                int idColumn        = calendarCursor.getColumnIndex("_id");
                do {
                    String calName  = calendarCursor.getString(nameColumn);
                    String calId    = calendarCursor.getString(idColumn);
                    if (calName != null /*&& calName.contains("Test")*/) {
                        calendar_id = Integer.parseInt(calId);
                    }
                } while (calendarCursor.moveToNext());
            }
            return calendar_id;
        }

private void saveCalendarEventICS() {

            Intent intent = new Intent(Intent.ACTION_INSERT)
             .setType("vnd.android.cursor.item/event")
             .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, frsttime)
             .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, sncdtime)
             .putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY , false) // just included for completeness
             .putExtra(Events.TITLE, vl_hldr[0])
             .putExtra(Events.DESCRIPTION, vl_hldr[2])
             .putExtra(Events.EVENT_LOCATION, vl_hldr[1])
             .putExtra(Events.RRULE, "FREQ=DAILY;COUNT=10") 
             .putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY)
             .putExtra(Events.ACCESS_LEVEL, Events.ACCESS_PRIVATE)
             .putExtra(Events.ALLOWED_REMINDERS, "METHOD_DEFAULT")
             .putExtra(Intent.EXTRA_EMAIL, "");
            startActivity(intent);
        }


private void saveCalendarEvent(int calid) {
            // TODO Auto-generated method stub

//Create the event here -----------


                Uri newEvent;
                if (Build.VERSION.SDK_INT >= 8) {
                    //newEvent = Uri.parse("content://com.android.calendar/events");
                    newEvent = ctx.getContentResolver().insert(Uri.parse("content://com.android.calendar/events"), event);

                    if (newEvent != null) {
                        long id = Long.parseLong( newEvent.getLastPathSegment() );
                        ContentValues values = new ContentValues();
                        values.put( "event_id", id );
                        values.put( "method", 1 );
                        values.put( "minutes", 15 ); // 15 minuti

                        getContentResolver().insert( Uri.parse( "content://com.android.calendar/reminders" ), values );
                    }
                }
                else {
                    newEvent = ctx.getContentResolver().insert(Uri.parse("content://calendar/events"), event);

                    if (newEvent != null) {
                        long id = Long.parseLong( newEvent.getLastPathSegment() );
                        ContentValues values = new ContentValues();
                        values.put( "event_id", id );
                        values.put( "method", 1 );
                        values.put( "minutes", 15 ); // 15 minuti

                        getContentResolver().insert( Uri.parse( "content://calendar/reminders" ), values );
                    }
                }

            }catch(Exception ee){}

        }

关于android - 各种 API 上的日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12202105/

相关文章:

android - 通过 Intent 在 Activity 之间传递 JSON 数组

android - 打开 Activity 的进度对话框

java - java中如何计算每月的出现次数

ios - Objective c 或 Swift 中的自定义日期(在范围内)选择日历

android - 银河 s3 模拟器

java - 获取每个值的 JSON 数组

javascript - 同时将事件绑定(bind)到多个 jquery 元素

c# - 在 C# 中引发事件的单元测试(按顺序)

c# - 使用 MulticastDelegate 作为参数同时避免 DynamicInvoke

javascript - 使用多个日历时完整日历事件消失 - Meteor