Android 添加事件和提醒在6.0棉花糖中不起作用
我面临着一个奇怪的问题。 我正在尝试在Android 添加事件和提醒在6.0棉花糖中不起作用,android,events,calendar,reminders,Android,Events,Calendar,Reminders,我面临着一个奇怪的问题。 我正在尝试在日历和提醒中添加事件。它在所有设备中都能正常工作,除了有棉花糖-6.0 当我尝试添加事件时,它还会在此处返回事件id。即使我在这里获得了事件id,我也看不到日历中添加的事件 ContentValues event = new ContentValues(); event.put("calendar_id", 1); event.put("title", "Match"); event.put("descr
日历
和提醒
中添加事件。它在所有设备中都能正常工作,除了有棉花糖-6.0
当我尝试添加事件时,它还会在此处返回事件id。即使我在这里获得了事件id,我也看不到日历中添加的事件
ContentValues event = new ContentValues();
event.put("calendar_id", 1);
event.put("title", "Match");
event.put("description",PTGConstantMethod.validateString(match.getMatch_name()));
event.put("eventLocation", match.getVenue());
event.put("eventTimezone", TimeZone.getDefault().getID());
event.put("dtstart", startDate.getTime());
event.put("dtend", endDate);
event.put("allDay", 0); // 0 for false, 1 for true
event.put("eventStatus", 1);
event.put("hasAlarm", 1); // 0 for false, 1 for true
Uri eventUri = context.getApplicationContext()
.getContentResolver()
.insert(Events.CONTENT_URI, event);
long eventID = Long.parseLong(eventUri.getLastPathSegment());
我在这里加上提醒,
ContentValues reminders = new ContentValues();
reminders.put("event_id", eventID);
reminders.put("method", CalendarContract.Reminders.METHOD_ALERT);
reminders.put("minutes", minutes);
context.getApplicationContext().getContentResolver()
.insert(Reminders.CONTENT_URI, reminders);
但应用程序在添加提醒时崩溃。这是我的日志
android.database.sqlite.SQLiteException
在android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:179)中
在android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)中
位于android.content.ContentProviderProxy.insert(ContentProviderNative.java:476)
位于android.content.ContentResolver.insert(ContentResolver.java:1231)
以我的身材,格雷德尔
minSdkVersion 14
targetSdkVersion 22
我最终发现没有任何带有event.put(“calendar\u id”,1)的日历代码>。那么,我如何获得应用程序默认日历
我尝试添加新的日历并向其中添加事件
ContentValues calendar = new ContentValues();
calendar.put(CalendarContract.Calendars.NAME, context.getResources().getString(R.string.app_name));
calendar.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, context.getResources().getString(R.string.app_name)+" Calendar");
calendar.put(CalendarContract.Calendars.VISIBLE, true);
Uri uri = context.getApplicationContext()
.getContentResolver()
.insert(CalendarContract.Calendars.CONTENT_URI, calendar);
long calendarId = Long.parseLong(uri.getLastPathSegment());
但我无法在日历应用程序中查看此日历。当我尝试使用此新日历添加事件时。事件得到显示,但点击事件,我的默认日历应用程序崩溃。将新日历添加到默认日历应用程序时,我做错了什么?谢谢。很抱歉以这种方式发布答案,但我在Android M calendar中手动添加事件时发现,它说我们需要将帐户添加到calendar帐户才能添加事件。所以我猜测,除非我们不将日历添加或映射到至少一个帐户,否则我们无法手动或以编程方式将事件添加到Android M设备日历 试试下面的链接:
也许你想使用内部片段
首次使用时:
super.requestPermissions( new String[]{Manifest.permission.WRITE_CALENDAR}, MY_PERMISSIONS_REQUEST_WRITE_CALENDAR);
在fragment内部,您需要调用:
FragmentCompat.requestPermissions(permissionsList, RequestCode)
注意:
ActivityCompat.requestPermissions(Activity, permissionsList, RequestCode);
在gradle中为FragmentCompat类添加此库
compile 'com.android.support:support-v13:version_of_library'
棉花糖需要用户的许可,这是从Android M开始引入的一项新功能。实现许可管理器来授予访问权限并执行。是的。我知道这一点,但如果我的目标sdk是23,这是正确的吗?但我的目标sdk是22。不管怎样,但你是直接调试到棉花糖对吗?因此,如果使用棉花糖sdk,代码也需要更改为支持工作,但如果这是原因,那么我应该检查其他权限,如摄像头、Gps和其他。但这些功能运行良好。所以我很困惑。让我检查一下权限。我不认为这是6.0中的权限问题。已检查日历的权限,并且已授予该权限。如何以编程方式解决该问题?我有同样的@Beena问题,但是在用户设备上,因此我不能强迫他/她配置帐户。