Googleカレンダーを作成しようとしていますが、カレンダーを作成できましたが、Googleと同期していませんでした。同期Googleカレンダーを作成できません。Android
多かれ少なかれ問題がどこにあるのか分かりますが、修正できません。
私が使用するコードは、このです:
public static long createCalendar (Activity activity, String name, String account, boolean local){
String color = "blue";
ContentValues calendarvalues = new ContentValues();
//The account that was used to sync the entry to the device. If the account_type is not {@link #ACCOUNT_TYPE_LOCAL} then the name and
// type must match an account on the device or the calendar will be deleted.
if(local) {
calendarvalues.put(CalendarContract.Calendars.ACCOUNT_NAME, "DUMMYLOCAL");
calendarvalues.put(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
}else{
calendarvalues.put(CalendarContract.Calendars.ACCOUNT_NAME, account);
calendarvalues.put(CalendarContract.Calendars.ACCOUNT_TYPE, account);
}
//Local CalendarContract.ACCOUNT_TYPE_LOCAL
calendarvalues.put(CalendarContract.Calendars.NAME, name);
calendarvalues.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, name);
calendarvalues.put(CalendarContract.Calendars.CALENDAR_COLOR, Color.parseColor(color));
calendarvalues.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);
// //None CalendarContract.Calendars.CAL_ACCESS_NONE Cannot access the calendar
// //freeBusy CalendarContract.Calendars.CAL_ACCESS_FREEBUSY Can only see free/busy information about the calendar
// //Read CalendarContract.Calendars.CAL_ACCESS_READ Can read all event details
// //Respond CalendarContract.Calendars.CAL_ACCESS_RESPOND Can reply yes/no/maybe to an event
// //Override CalendarContract.Calendars.CAL_ACCESS_OVERRIDE not used
// //Contributor CalendarContract.Calendars.CAL_ACCESS_CONTRIBUTOR Full access to modify the calendar, but not the access control settings
// //Editor CalendarContract.Calendars.CAL_ACCESS_EDITOR Full access to modify the calendar, but not the access control settings
// //Owner CalendarContract.Calendars.CAL_ACCESS_OWNER Full access to the calendar
// //Root CalendarContract.Calendars.CAL_ACCESS_ROOT Domain admin
calendarvalues.put(CalendarContract.Calendars.OWNER_ACCOUNT, account);
calendarvalues.put(CalendarContract.Calendars.VISIBLE, 1);
calendarvalues.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
// calendarvalues.put(CalendarContract.Calendars.CALENDAR_LOCATION, "Spain");
Uri calUri = null;
Uri result = null;
if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
PermissionUtil.requestCalendarPermission(activity);
return -1;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
calUri = CalendarContract.Calendars.CONTENT_URI;
}else{
calUri = Uri.parse("content://com.android.calendar/calendars");
}
if(calUri != null) {
if(local) {
calUri = calUri.buildUpon()
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, "DUMMYLOCAL")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL)
.build();
}else {
calUri = calUri.buildUpon()
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, account)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, account)
.build();
}
result = activity.getContentResolver().insert(calUri, calendarvalues);
}
if (result != null) {
try {
return Long.parseLong(result.getLastPathSegment());
} catch (Exception e) {
return -1;
}
}
return -1;
}
私は間違いは、この行であると思う:
calendarvalues.put(CalendarContract.Calendars.ACCOUNT_TYPE, account);
私は次のクエリによって返される値を見てきましたので:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
calUri = CalendarContract.Calendars.CONTENT_URI;
}else{
calUri = Uri.parse(calendarUriString);
}
String[] projection = new String[]{
CalendarContract.Calendars._ID,
CalendarContract.Calendars.NAME,
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL,
CalendarContract.Calendars.ACCOUNT_NAME,
CalendarContract.Calendars.ACCOUNT_TYPE,
// CalendarContract.Calendars.CALENDAR_COLOR,
CalendarContract.Calendars.OWNER_ACCOUNT,
CalendarContract.Calendars.VISIBLE,
CalendarContract.Calendars.SYNC_EVENTS,
};
Cursor cursor = activity.getContentResolver().query(calUri, projection, null, null, null);
であり、結果は以下の通りである:
ID:1
名:マイカレンダー
アクセスレベル:700
アカウント名:
ローカルAccountType @マイカレンダー:com.local
ownerAccount:目に見える所有者アカウント:1ローカル
表示名@マイカレンダー
同期:1
ID:2
名:[email protected]
表示名:[email protected]
アクセスレベル:700
アカウント名:[email protected]
AccountType:
com.google ownerAccount:[email protected]
可視:1
同期:1
ID:3
名:テストカル
表示名:テストカル
アクセスレベル:700
アカウント名:[email protected]
AccountType:[email protected]
ownerAccount:1
表示:1
同期:
calendarvalues.put(CalendarContract.Calendars.ACCOUNT_TYPE, "com.google");
が、カレンダーが作成されていません:1
私は次のように入れてみました。
誰かがそれを行う方法を知っているか、有用である可能性のある例や文書があれば、それが役に立ちます。
ありがとうございます。
でアカウントの同期を強制します? – noogui
@noogui私は、ローカルではなくGoogleアカウントと同期するカレンダーを作成しようとしていますが、後でイベントを作成し、これをGoogleアカウントと同期させると、デバイスごとに作成する必要はありません。オプションを選択するインテントがなくても、ユーザーには透過的でなければなりません。 – Joacer