获取CALENDAR_ID以在Google日历中插入活动

问题描述 投票:1回答:1

我正在尝试在我的Google日历中插入一个事件,但到目前为止还没有。

有没有一种简单的方法可以在我的方法中获得正确的CALENDAR_ID?

 private void syncwithgooglecalendar2() {
    Account[] accounts = AccountManager.get(CalendarSyncActivity.this).getAccountsByType(
            "com.google");
    String accountName = "";
    for (Account acc : accounts) {
        Log.d(">>>>>>>", "Name: " + acc.name + "-----Type: " + acc.type);
        accountName = acc.name;
    }
    long calID = getCalendarId(CalendarSyncActivity.this, accountName);
    ....
}

private long getCalendarId(Activity activity, String MY_ACCOUNT_NAME) {
    String[] projection = new String[]{BaseColumns._ID};
    String selection = CalendarContract.Calendars.ACCOUNT_NAME + " = ?";
    String[] selArgs = new String[]{MY_ACCOUNT_NAME};
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        return TODO;
    }
    Cursor cursor = activity.getContentResolver().query(
            CalendarContract.Calendars.CONTENT_URI, projection, selection, selArgs, null);
    if (cursor.moveToFirst()) {
        return cursor.getLong(0);
    }
    return -1;
}

Log给出了以下信息:

    D/>>>>>>>: Name: [email protected]: com.google
    D/>>>>>>>: Name: [email protected]: com.google
    D/CalId:: 4

我期待另一个CalId,但我不确定...当日历中有多个帐户连接时是否存在问题?我读到了“主要”,但我不知道如何在现有代码中安装它。或者还有另一种解决方法吗?

android google-calendar-api
1个回答
0
投票

您的calendarID通常是您的电子邮件地址。但是如果你有几个日历,你可以使用calendars.list方法来获取它们。

© www.soinside.com 2019 - 2024. All rights reserved.