1

我想让应用程序在 Android 2.2 和最新版本上运行。

当我想创建一个将事件插入日历的功能时,它无法实现。如何使以下功能起作用?

我使应用程序在具有 MinSDK 级别 8 的 Android 2.2 上运行

<uses-sdk android:minSdkVersion="8" />

我无法导入以下库:

import android.provider.CalendarContract;
import android.provider.CalendarContract.Events;

private void saveCalendarEventICS(ArrayList<EventItem> event, int position) {

        Intent intent = new Intent(Intent.ACTION_INSERT)
         .setType("vnd.android.cursor.item/event")
         .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, "1111111")
         .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, "222222")
         .putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY , false) // just included for completeness
         .putExtra(Events.TITLE,event.get(position).title)
         .putExtra(Events.DESCRIPTION, "dcscdsvvd")
         .putExtra(Events.EVENT_LOCATION, "Kuala Lumpur")
         .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);
    }
4

2 回答 2

0

在你的代码中试试这个:

Calendar cal = Calendar.getInstance();              
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", cal.getTimeInMillis());
intent.putExtra("allDay", true);
intent.putExtra("rrule", "FREQ=YEARLY");
intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
intent.putExtra("title", "A Test Event from android app");
startActivity(intent);

编辑:

检查此代码 http://code.google.com/p/android-calendar-provider-tests/source/browse/trunk/src/com/androidbook/androidcalendar/CalendarActivity.java

于 2013-01-25T08:33:27.887 回答
0

在 Ics 中引入了一个新的 Calendar api,因此您的代码将无法在 ics 中运行。ICS 中的新公共 API

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

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){}

    }
于 2013-01-25T08:48:38.463 回答