如何在按钮单击时将事件添加到Android默认日历?

时间:2013-01-17 14:30:12

标签: android events calendar cursor android-contentresolver

protected void addToCalendar()
    {
        final ContentResolver cr = this.getContentResolver();
        Cursor cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), new String[]{ "_id", "displayName" }, null, null, null);

        cursor.moveToFirst();
            String[] calNames = new String[cursor.getCount()];
        final int[] calIds = new int[cursor.getCount()];
        Log.i("Cal Names Length", "Length of Cals:"+calNames.length);
        Log.i("Names of calendar:", "Cal names:"+calNames.toString());
        for (int i=0; i<calNames.length; i++)
        {
            calIds [i] = cursor.getInt(0);
            calNames[i] = cursor.getString(1);
            cursor.moveToNext();
            Log.i("Calendar", "Cal_id:"+calIds[0]);
            Log.i("Calendar", "Cal_Name:"+calNames[0]);
        }
        cursor.close();

        ContentResolver cr = getContentResolver();

        if(calIds.length > 0)
        {
            Log.i("in if condition", "Of calIds.length");
            Calendar cal =  Calendar.getInstance(); 
            Date birth_date = null;
            for (int i = 0; i < final_list.size(); i++)
            {
                    obj = final_list.get(i);
                    String dateFromDB = obj.getmBirthday();

                    SimpleDateFormat parser = new SimpleDateFormat("MM/dd/yyyy");
                    try 
                    {
                         birth_date = parser.parse(dateFromDB);
                    } 

                    catch (java.text.ParseException e)
                    {
                        e.printStackTrace();
                    }


                 // event insert
                    int cal_id = calIds[0];
                    ContentValues values = new ContentValues();
                    values.put("calendar_id", cal_id);
                    Log.i("Calendar", "Cal_ID:"+cal_id);
                    values.put("title", obj.getmName());
                    Log.i("Title", "Title:"+obj.getmName());

                    values.put("dtstart", birth_date.getTime());

                    values.put("dtend",  birth_date.getTime() + 1800*1000);

                    values.put("duration",  1800*1000 );
                    values.put("description", "Birthday Reminder");
                    values.put("allDay", 1);
                    values.put("rrule", "FREQ=YEARLY");
                    values.put("visibility", 0);
                    values.put("hasAlarm", 1);
                    cr.insert(Uri.parse("content://com.android.calendar/events"), values);


            }

        }

    }

我在点击按钮时使用了这种方法。当我点击按钮时,ArrayList of Events应添加到Android设备的默认日历中。但是在这段代码中,我无法看到添加到Calendar的事件。我甚至认为这个条目没有被添加到日历中。 谢谢。

1 个答案:

答案 0 :(得分:3)

这是我使用的,它适用于我的代码

Intent intent = new Intent(Intent.ACTION_EDIT);  
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", startDate.getTimeInMillis());
intent.putExtra("endTime", endDate.getTimeInMillis());
intent.putExtra("title", title);
activty.startActivity(intent);

该事件已添加到Android日历中,但未检查是否已存在相同的事件(可能是事件的重复)