R
R
ramovich2020-08-13 07:05:12
Java
ramovich, 2020-08-13 07:05:12

How to explicitly specify in Google Calendar Android how many minutes before the event to call the reminder?

I add an event to the google calendar from my android application as follows

Intent intent = new Intent(Intent.ACTION_INSERT)
                                .setData(CalendarContract.Events.CONTENT_URI)
                                .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, millis)
                                .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, millis + 60 * 60 * 1000)
                                .putExtra(CalendarContract.Events.TITLE, evtext)
                                .putExtra(CalendarContract.Events.DESCRIPTION, evtext)
                                .putExtra(CalendarContract.Events.HAS_ALARM, true)
                                .putExtra(CalendarContract.Reminders.EVENT_ID, CalendarContract.Events._ID)
                                .putExtra(CalendarContract.Events.ALLOWED_REMINDERS, "METHOD_DEFAULT")
                                .putExtra(CalendarContract.Reminders.MINUTES, 1)
                                .putExtra(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT)
                                .putExtra(CalendarContract.Events.AVAILABILITY, CalendarContract.Events.AVAILABILITY_BUSY);

                        startActivity(intent);


Everything works, only the notification of the event pops up 30 minutes before the event itself
. How can I programmatically specify that I want to receive a notification, say, 1 minute before the event?

.putExtra(CalendarContract.Reminders.MINUTES, 1)

for some reason does not work
Android 8.1

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
ramovich, 2020-08-13
@ramovich

in general, I solved the issue without activity, by adding an explicit request for permission to write to the calendar

public void addEvent(Context context, long dat1, long dat2, String titl) {

            try {
                ContentResolver cr = context.getContentResolver();
                ContentValues values = new ContentValues();
                values.put(CalendarContract.Events.DTSTART, dat1);
                values.put(CalendarContract.Events.DTEND, dat2);
                values.put(CalendarContract.Events.TITLE, titl);
                values.put(CalendarContract.Events.CALENDAR_ID, 1);
                values.put(CalendarContract.Events.EVENT_TIMEZONE, Calendar.getInstance()
                        .getTimeZone().getID());
                System.out.println(Calendar.getInstance().getTimeZone().getID());
                Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);

                // Save the eventId into the Task object for possible future delete.
                long meventId = Long.parseLong(uri.getLastPathSegment());
                // Add a 5 minute, 1 hour and 1 day reminders (3 reminders)
                setReminder(cr, meventId, 1);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        // routine to add reminders with the event
        public void setReminder(ContentResolver cr, long eventID, int timeBefore) {
            try {
                ContentValues values = new ContentValues();
                values.put(CalendarContract.Reminders.MINUTES, timeBefore);
                values.put(CalendarContract.Reminders.EVENT_ID, eventID);
                values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
                Uri uri = cr.insert(CalendarContract.Reminders.CONTENT_URI, values);
                Cursor c = CalendarContract.Reminders.query(cr, eventID,
                        new String[]{CalendarContract.Reminders.MINUTES});
                if (c.moveToFirst()) {
                    System.out.println("calendar"
                            + c.getInt(c.getColumnIndex(CalendarContract.Reminders.MINUTES)));
                }
                c.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

if (checkSelfPermission(Manifest.permission.WRITE_CALENDAR)
                                == PackageManager.PERMISSION_GRANTED) {
                            addEvent(getApplicationContext(), millis, (millis + 60 * 60 * 1000), evtext);
                            otvet="Событие добавлено";
                        } else {
                            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_CALENDAR}, 1);
                            otvet="Разреши мне доступ к календарю чтобы добавить напоминание, а после повтори команду устанавливающую напоминание еще раз.";
                        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question