我可以在 android 中将事件添加到日历而不让用户知道吗?

Can I add event to calendar without letting user know in android?

我想使用以下代码将事件添加到我的本机日历。它工作正常。

SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm");
                final Calendar cal = Calendar.getInstance();
                try {
                    cal.setTime(formatter.parse(datetime));
                    eventdate = cal.get(Calendar.YEAR)+"/"+cal.get(Calendar.MONTH)+"/"+cal.get(Calendar.DAY_OF_MONTH)+" "+cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE);
                    Log.e("event date",eventdate);
                } 
                catch (ParseException e) {
                    e.printStackTrace();
                }
                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("endTime", cal.getTimeInMillis()+60*60*1000);
                intent.putExtra("title", title);
                intent.putExtra("description",desc);
                c.startActivity(intent);

这会打开一个屏幕,要求我保存活动。当我点击保存时,它会将我的活动保存到日历中。

现在,我想直接在原生日历中添加事件,而不是显示上面的屏幕。有什么办法可以做到吗?

您可以参考下面的代码在日历中添加事件。

String GLOBAL_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
String gameDate="2015-07-12 01:10:00";
        Date startDate = DateConstants.getDateFromString(
                gameDate,GLOBAL_DATE_FORMAT);
        long endDate = startDate.getTime()
                + (PTGConstantMethod.validateInteger(game
                        .getGame_duration()) * 1000);

        ContentValues event = new ContentValues();
        event.put("calendar_id", 1);
        event.put("title", "Game#" + game.getId());
        event.put("description", game.getLocation());
        event.put("eventLocation", game.getLocation());
        event.put("eventTimezone", TimeZone.getDefault().getID());
        event.put("dtstart", startDate.getTime());
        event.put("dtend", endDate);

        event.put("allDay", 0); // 0 for false, 1 for true
        event.put("eventStatus", 1);
        event.put("hasAlarm", 1); // 0 for false, 1 for true

        String eventUriString = "content://com.android.calendar/events";
        Uri eventUri = context.getApplicationContext()
                .getContentResolver()
                .insert(Uri.parse(eventUriString), event);
        long eventID = Long.parseLong(eventUri.getLastPathSegment());

// if reminder need to set


            int minutes=120;


            // add reminder for the event
            ContentValues reminders = new ContentValues();
            reminders.put("event_id", eventID);
            reminders.put("method", "1");
            reminders.put("minutes", minutes);

            String reminderUriString = "content://com.android.calendar/reminders";
            context.getApplicationContext().getContentResolver()
            .insert(Uri.parse(reminderUriString), reminders);
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("endTime", cal.getTimeInMillis()+60*60*1000);
                intent.putExtra("title", title);
                intent.putExtra("description",desc);
                c.startActivity(intent);

不要使用它,因为在这里调用它 c.startActivity(intent); 您正在启动一个 activity,这将让用户在日历中并要求保存它而不是使用它 您可以使用 ContentResolver 插入包含您的信息的事件

像这样

            try
            {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm");
Calendar cal = Calendar.getInstance();
    cal.setTime(formatter.parse(datetime));
                        eventdate = cal.get(Calendar.YEAR)+"/"+cal.get(Calendar.MONTH)+"/"+cal.get(Calendar.DAY_OF_MONTH)+" "+cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE);
                        Log.e("event date",eventdate);
    // provide CalendarContract.Calendars.CONTENT_URI to
    // ContentResolver to query calendars
            Cursor      cur = this.getContentResolver().query(CalendarContract.Calendars.CONTENT_URI,null, null, null, null);
                if (cur.moveToFirst())
                {
                    long calendarID=cur.getLong(cur.getColumnIndex(CalendarContract.Calendars._ID));
    ContentValues eventValues = new ContentValues();
    // provide the required fields for creating an event to
    // ContentValues instance and insert event using
    // ContentResolver
            eventValues.put (CalendarContract.Events.CALENDAR_ID, calendarID);
            eventValues.put(CalendarContract.Events.TITLE, "Event 1"+title);
            eventValues.put(CalendarContract.Events.DESCRIPTION," Calendar API"+desc);
eventValues.put(CalendarContract.Events.ALL_DAY,true);
            eventValues.put(CalendarContract.Events.DTSTART, (cal.getTimeInMillis()+60*60*1000));
            eventValues.put(CalendarContract.Events.DTEND, cal.getTimeInMillis()+60*60*1000);
            eventValues.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().toString());
            Uri eventUri = this.getContentResolver().insert(CalendarContract.Events.CONTENT_URI, eventValues);
            eventID = ContentUris.parseId(eventUri);
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            finally
            {
                if (cur != null)
                {
                    cur.close();
                }
            }

在您的保存事件按钮中写入此代码,它将按照您的方式工作