【问题标题】:Inserting event to Google Calendar in Android在 Android 中将事件插入 Google 日历
【发布时间】:2019-06-25 05:34:46
【问题描述】:

我正在制作一个允许用户通过他们的 Android 应用程序插入事件的应用程序。我的应用程序使用适用于 Android 的 Google 日历 API,我目前很困惑,我的应用程序只允许我自己的帐户将事件插入 Google 日历。谁能帮我解决我的问题。如果您想知道,这是代码

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");
        }
    }
}

【问题讨论】:

  • 我认为您的帐户只允许您将事件插入自己的日历而不是其他人的日历是有意义的。那么有什么问题呢?
  • 当我在其他手机上使用其他账号时,事件不会添加,这是问题。

标签: android api google-calendar-api


【解决方案1】:

尝试添加以下代码(除了 IOException);它会弹出一个单独的提示,用户向您的应用提供修改用户日历的权​​限:

catch (UserRecoverableAuthIOException e) {
    context.startActivity(e.getIntent());
}

请注意,这会在用户的 Google 帐户上触发安全警报,这是应该的。不过,我没有看到任何负面影响,也不需要他们额外确认。根据帐户设置,可能有一种方法可以让 google 帐户不允许此权限,并且可能永远不会触发异常;最好在那个时候失败。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    • 1970-01-01
    • 2011-03-27
    • 2015-09-05
    • 1970-01-01
    相关资源
    最近更新 更多