0

I'm launching a calendar-insertion activity using the following:

Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setType("vnd.android.cursor.item/event");

Calendar cal = Calendar.getInstance();
long startTime = cal.getTimeInMillis();
long endTime = startTime + 60 * 60 * 1000;

intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startTime);
intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime);
intent.putExtra(Events.TITLE, "Frank Zappa Live");
intent.putExtra(Events.EVENT_LOCATION, "Madison Square Garden");

startActivity(intent);

So far so good. But how can I know if the user opted to cancel?

Ariel Malka
  • 15,697
  • 6
  • 31
  • 33
  • This code is kind of broken as is. It assumes they use the default calendar app. Many users don't. You should never be assuming application names like that.The correct way to add to a calendar can be seen at https://stackoverflow.com/questions/13709477/how-to-add-calendar-events-to-default-calendar-silently-without-intent-in-andr and it doesn't use an intent, so can't be canceled. – Gabe Sechan Dec 03 '19 at 16:13
  • @GabeSechan: I don't want to insert an event silently. By design, I must leave the choice to the user to cancel. – Ariel Malka Dec 03 '19 at 16:33
  • Then you should pop up your own dialog to do that. MANY users don't use, or even have installed, that calendar. It won't work on about 50% of users. The correct way is via the link I showed you. If you need a confirmation dialog, do that first – Gabe Sechan Dec 03 '19 at 21:41

1 Answers1

1

You should use startActivityForResult and you will get Activity#RESULT_OK or Activity#RESULT_CANCEL as a result.

From : https://developer.android.com/reference/android/provider/CalendarContract#ACTION_HANDLE_CUSTOM_EVENT