Reputation: 21
I'm making an application that allows the user to insert an event through their Android application. My application uses Google Calendar API for Android, and I'm currently confused, my app only allows my own account to insert events to Google Calendar. Can anyone help me solve my problem. Here's the code if u wondering
private class MakeNotificationTask extends AsyncTask<Void, Void, Boolean>{
private com.google.api.services.calendar.Calendar mService = null;
private Exception mLastError = null;
public MakeNotificationTask(GoogleAccountCredential credential) {
HttpTransport transport = AndroidHttp.newCompatibleTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
mService = new com.google.api.services.calendar.Calendar.Builder(
transport, jsonFactory, credential)
.setApplicationName("Google Calendar API Android Quickstart")
.build();
}
public void MakeNotification(String name, String date){
Event event = new Event()
.setSummary(name + "'s vehicle registration license expires")
.setDescription("Please renew your vehicle registration license!!!");
String newId = generateTimeStamp();
event.setId(newId);
VehiclesDatabase dbase = new VehiclesDatabase(getApplicationContext());
dbase.insertSTNK(newId, name);
DateTime startDateTime = new DateTime(date+"T09:00:00+07:00");
EventDateTime start = new EventDateTime()
.setDateTime(startDateTime);
event.setStart(start);
DateTime endDateTime = new DateTime(date+"T23:59:59+07:00");
EventDateTime end = new EventDateTime()
.setDateTime(endDateTime);
event.setEnd(end);
EventReminder[] reminderOverrides = new EventReminder[] {
new EventReminder().setMethod("popup").setMinutes(14 * 24 * 60),
new EventReminder().setMethod("popup").setMinutes(28 * 24 * 60),
};
Event.Reminders reminders = new Event.Reminders()
.setUseDefault(false)
.setOverrides(Arrays.asList(reminderOverrides));
event.setReminders(reminders);
String calendarId = "primary";
try {
mService.events().insert(calendarId, event).execute();
Log.e("event making", "success");
} catch (IOException e) {
Log.e("event making", "goes horribly wrong "+e.getMessage());
e.printStackTrace();
}
}
@Override
protected Boolean doInBackground(Void... params) {
try {
MakeNotification(vehicleName, stnkDate);
Log.e("event insertion success", "Success in making event");
return true;
} catch (Exception e) {
mLastError = e;
Log.e("event insertion error", "Failed in making event " + e.getMessage());
return false;
}
}
@Override
protected void onCancelled() {
if (mLastError != null) {
if (mLastError instanceof GooglePlayServicesAvailabilityIOException) {
showGooglePlayServicesAvailabilityErrorDialog(
((GooglePlayServicesAvailabilityIOException) mLastError)
.getConnectionStatusCode());
} else if (mLastError instanceof UserRecoverableAuthIOException) {
startActivityForResult(
((UserRecoverableAuthIOException) mLastError).getIntent(),
AddVehicles.REQUEST_AUTHORIZATION);
} else {
Log.e("error when cancelled", mLastError.getMessage());
}
} else {
Log.e("cancelled", "request cancelled");
}
}
}
Upvotes: 2
Views: 456
Reputation: 1744
Try adding the following code (in addition to the IOException); it brings up a separate prompt which the user provides your App permission to modify the user's calendar:
catch (UserRecoverableAuthIOException e) {
context.startActivity(e.getIntent());
}
Please be aware that this will trigger a security alert on the user's google account, as it kind of should. I haven't seen any negative consequences to it, though, and haven't required additional confirmation on their part. Depending on account setup, there is probably a way to have a google account that just won't allow this permission, and might never trigger the exception; best to just fail at that time.
Upvotes: 0