0

我想创建日历。这是工作。但是如果安卓版本+4.0。这行不通。我知道我需要使用内容提供者,但我不知道如何。我希望该日历适用于所有版本的 Android。我该怎么做?

   private void AddCalendar(int years,int months,int days,int hours,int minutes){
        try{
        // get calendar
        Calendar cal = Calendar.getInstance();     
        Uri EVENTS_URI = Uri.parse("content://com.android.calendar/events");
        ContentResolver cr = getContentResolver();

        Calendar start = Calendar.getInstance();
        start.set(years, months, days, hours, minutes, 0);

        Calendar end = Calendar.getInstance();
        end.set(years, months, days, hours + 1, minutes, 0);

        long startTime = start.getTimeInMillis();       
        long endTime = end.getTimeInMillis();

        // event insert
        ContentValues values = new ContentValues();
        values.put("calendar_id", 1);
        values.put("title", editText1.getText().toString());
        values.put("allDay", 0);
        values.put("dtstart", startTime); 
        values.put("dtend", endTime); 
        values.put("description", editText2.getText().toString());
        values.put("visibility", 0);
        values.put("hasAlarm", 1);
        Uri event = cr.insert(EVENTS_URI, values);

        // reminder insert
        Uri REMINDERS_URI = Uri.parse("content://com.android.calendar/reminders");
        values = new ContentValues();
        values.put( "event_id", Long.parseLong(event.getLastPathSegment()));
        values.put( "method", 1 );
        values.put( "minutes", 10 );
        cr.insert( REMINDERS_URI, values );

        }catch (Exception e){
            Toast.makeText(getApplicationContext(), 
                    R.string.calendar, Toast.LENGTH_LONG).show();
        }

        }
4

2 回答 2

1

Calendar Api 在 4.0+ 中发生了变化。您可以使用新的 Calendar Intent api(这是推荐的方法)或修改您的活动以适应新的 api:

values.add("eventTimezone","00"); 删除“可见性”键/值,它不再被接受。

您应该在 ExceptionCatcher 中打印异常,它具有很强的描述性。

于 2012-09-13T15:25:48.630 回答
0

Here it is answered and working!

But change the code to:

values.put(CalendarContract.Events.CALENDAR_ID, 1);

1 instead of 3 in order to select the default calendar.

But you can't have the same code for all android versions! You will need to make 2 different apps.One for android 4+ and one for android<4

于 2012-12-12T12:01:22.547 回答