Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 将事件添加到本机日历不起作用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个 Android应用程序.我必须将事件添加到原生Android日历中.所以我尝试了以下代码
if (Build.VERSION.SDK_INT >= 8 ) {
    l_eventUri = Uri.parse("content://com.android.calendar/events");
} else {
    l_eventUri = Uri.parse("content://calendar/events");
}

cursor cursor = getContentResolver() .query(Uri.parse(getCalendarUriBase(this)),new String[] { "calendar_id","displayname"},null,null);
cursor.moveToFirst();
// fetching calendars name
String CNames[] = new String[cursor.getCount()];
// fetching calendars id
int[] CalIds = new int[cursor.getCount()];
for (int i = 0; i < CNames.length; i++) {
    CalIds[i] = cursor.geTint(0);
    CNames[i] = cursor.getString(1);
    cursor.moveToNext();
}

// get calendar
Calendar cal = Calendar.geTinstance();     
Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this) + "events");
ContentResolver cr = getContentResolver();

// event insert
ContentValues values = new ContentValues();
values.put("calendar_id",111);
values.put("title","Reminder title");
values.put("allDay",0);
values.put("dtstart",cal.getTimeInMillis() + 11*60*1000); // event starts at 11 minutes from Now
values.put("dtend",cal.getTimeInMillis()+60*60*1000); // ends 60 minutes from Now
values.put("description","Reminder description");
//Event Id
values.put("_id",23);  
//0~ default; 1~ confidential; 2~ private; 3~ public
values.put("visibility",0);
//0~ false; 1~ true
values.put("hasAlarm",1);
//status: 0~ tentative; 1~ confirmed; 2~ canceled
values.put("eventStatus",1);
//0~ opaque,no timing conflict is allowed; 1~ transparency,allow overlap of scheduling
values.put("transparency",0);
Uri event = cr.insert(EVENTS_URI,values);

// reminder insert
Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this) + "reminders");
values = new ContentValues();
values.put( "event_id",Long.parseLong(event.getLastPathSegment()));
values.put( "method",1 );
values.put( "minutes",10 );
cr.insert( REMINDERS_URI,values );

private String getCalendarUriBase(Activity act) {
    String calendarUriBase = null;
    Uri calendars = Uri.parse("content://calendar/calendars");
    cursor managedcursor = null;
    try {
        managedcursor = act.managedQuery(calendars,null);
    } catch (Exception E) {
    }

    if (managedcursor != null) {
        calendarUriBase = "content://calendar/";
    } else {
        calendars = Uri.parse("content://com.android.calendar/calendars");
        try {
            managedcursor = act.managedQuery(calendars,null);
        } catch (Exception E) {
        }
        if (managedcursor != null) {
            calendarUriBase = "content://com.android.calendar/";
        }
    }
    return calendarUriBase;
}

当我运行此代码时,我收到IllegalArgumentexception.

java.lang.IllegalArgumentexception: UnkNown URL content://com.android.calendar/

我认为错误是因为getCalendarUriBase()方法.我搜索了很多其他方法,但到目前为止大多数开发人员都遵循上面的代码片段,因为这适用于每个Android版本.怎么解决

解决方法

当您尝试执行此行时,您错过了日历
cursor cursor = getContentResolver().query(
    Uri.parse(getCalendarUriBase(this)),/* <-- unkNown URL content */
    new String[] { "calendar_id","displayname" },null);

导致IllegalArgumentexception.

只需在getCalendarUriBase(this)之后添加日历即可.

cursor cursor = getContentResolver().query(
    Uri.parse(getCalendarUriBase(this)+"calendars"),new String[] { "_id","displayName" },null);

添加事件时,您需要包括:日历ID,开始日期,结束日期(或重复规则)和事件时区.除了时区,你已经包括了所有时区.尝试添加

values.put("eventTimezone",TimeZone.getDefault().getID());

在插入事件之前.

大佬总结

以上是大佬教程为你收集整理的android – 将事件添加到本机日历不起作用全部内容,希望文章能够帮你解决android – 将事件添加到本机日历不起作用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。