我正在尝试在我的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;
}
日志给了我以下信息:
D/>>>>>>>: Name: email1@gmail.com-----Type: com.google
D/>>>>>>>: Name: email2@gmail.com-----Type: com.google
D/CalId:: 4
我期待另一个CalId,但我不确定...... 如果有多个帐户与日历相关联,是否会出现问题?我读到了“主要”,但我不知道如何在现有代码中安装它。 还是有另一种解决方法吗?