插入日历事件和提醒

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (66)

我想通过我的android应用程序(android studio)在谷歌calender的添加提醒,但我已经尝试了一切,但它不会在日历中添加。我也尝试通过https://developer.android.com/guide/topics/providers/calendar-provider#events添加, 但仍然没有成功。

            Date d1 = new Date(1472570400);//Tue, 30 Aug 2016 15:20:00 GMT
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(d1);

        Date d2 = new Date(1472574000);//Tue, 30 Aug 2016 16:20:00 GMT
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(d2);

        Uri EVENTS_URI = Uri.parse("content://com.android.calendar/events");
        ContentResolver cr = getContentResolver();

        // event insert
        ContentValues values = new ContentValues();
        values.put("calendar_id", 1);
        values.put("title", "Reminder Title");
        values.put("allDay", 0);
        values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());                

        values.put("dtstart", cal1.getTimeInMillis() ); // event starts at Tue, 30 Aug 2016 15:20:00
        values.put("dtend", cal2.getTimeInMillis()); // ends at Tue, 30 Aug 2016 16:20:00 GMT

        System.out.println("ALARM TIMES START : " + cal1.getTimeInMillis());
        System.out.println("ALARM TIMES END : "+cal2.getTimeInMillis());

        values.put("description", "Reminder description");                
        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", 1); //Notify before the exact time
        cr.insert( REMINDERS_URI, values );
提问于
用户回答回答于
    public void insertCalendarDetail(String cal_id,String cal_date,String title,String desc,String cat_id)//,String cat_name
    {
        Uri event_id=null;
        String[] cal_dates = cal_date.split("-");
        Log.e("cal_dates==>"+cal_id,cal_dates[0]+"/"+cal_dates[1]+"/"+cal_dates[2]);

            java.util.Calendar startTime = java.util.Calendar.getInstance();
            startTime.set(Integer.parseInt(cal_dates[2]),Integer.parseInt(cal_dates[1])-1,Integer.parseInt(cal_dates[0]), 3, 00);
            long startMillis = startTime.getTimeInMillis();
            long endMillis = startTime.getTimeInMillis()+60 * 60 * 1000;;
            final ContentValues event = new ContentValues();
            event.put(CalendarContract.Events.CALENDAR_ID,1);
            event.put(CalendarContract.Events.TITLE,title);
            event.put(CalendarContract.Events.DESCRIPTION, Html.fromHtml(desc)+"");
            event.put(CalendarContract.Events.DTSTART, startMillis);
            event.put(CalendarContract.Events.DTEND, endMillis);
            String timeZone = TimeZone.getDefault().getID();
            event.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone);
            Uri baseUri;
            if (Build.VERSION.SDK_INT >= 8)
            {
                baseUri = Uri.parse("content://com.android.calendar/events");
            }
            else
            {
                baseUri = Uri.parse("content://calendar/events");
            }
            event_id = context.getContentResolver().insert(baseUri, event);
            Log.e("calendar_insert", event_id + "");

    }

扫码关注云+社区

领取腾讯云代金券