添加事件,本地日历不工作 [英] Adding events to native calendar is not working

查看:164
本文介绍了添加事件,本地日历不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Android应用程序。我要补充活动,以原生的Andr​​oid日历。所以,我想下面的code:

 如果(Build.VERSION.SDK_INT> = 8){
    l_eventUri = Uri.parse(内容://com.android.calendar/events);
}其他{
    l_eventUri = Uri.parse(内容://日历/事件);
}光标光标= getContentResolver().query(Uri.parse(getCalendarUriBase(本​​)),新的String [] {CALENDAR_ID,显示名称},NULL,NULL,NULL);
cursor.moveToFirst();
//获取日历名称
字符串的CNAME [] =新的String [cursor.getCount()];
//获取日历ID
INT [] = CalIds新INT [cursor.getCount()];
的for(int i = 0; I< CNames.length;我++){
    CalIds [I] = cursor.getInt(0);
    CNAMES [I] = cursor.getString(1);
    cursor.moveToNext();
}//获取日历
日历CAL = Calendar.getInstance();
乌里EVENTS_URI = Uri.parse(getCalendarUriBase(本​​)+事件);
ContentResolver的CR = getContentResolver();//事件插入
ContentValues​​值=新ContentValues​​();
values​​.put(CALENDAR_ID,111);
values​​.put(称号,提醒标题);
values​​.put(allDay,0);
values​​.put(DTSTART,cal.getTimeInMillis()+ 11 * 60 * 1000); //事件开始11分钟,从现在
values​​.put(DTEND,cal.getTimeInMillis()+ 60 * 60 * 1000); //从现在结束60分钟
values​​.put(说明,提醒说明);
//事件ID
values​​.put(_ ID,23);
// 0〜违约; 1〜保密; 2〜私有的; 3〜公
values​​.put(知名度,0);
// 0〜虚假的; 1〜真
values​​.put(hasAlarm,1);
//状态:0〜试探性的; 1〜证实; 2〜取消
values​​.put(eventStatus,1);
// 0〜不透明,没有时序冲突是允许的; 1〜透明度,让调度重叠
values​​.put(透明度,0);
乌里事件= cr.insert(EVENTS_URI,价值观);//插入提醒
乌里REMINDERS_URI = Uri.parse(getCalendarUriBase(本​​)+提醒);
值=新ContentValues​​();
values​​.put(事项标识,的Long.parseLong(event.getLastPathSegment()));
values​​.put(方法,1);
values​​.put(分钟,10);
cr.insert(REMINDERS_URI,价值观);私人字符串getCalendarUriBase(活动行为){
    字符串calendarUriBase = NULL;
    乌里日历= Uri.parse(内容://日历/日历);
    光标managedCursor = NULL;
    尝试{
        managedCursor = act.managedQuery(日历,NULL,NULL,NULL,NULL);
    }赶上(例外五){
    }    如果(managedCursor!= NULL){
        calendarUriBase =内容://日历/;
    }其他{
        日历= Uri.parse(内容://com.android.calendar/calendars);
        尝试{
            managedCursor = act.managedQuery(日历,NULL,NULL,NULL,NULL);
        }赶上(例外五){
        }
        如果(managedCursor!= NULL){
            calendarUriBase =内容://com.android.calendar/
        }
    }
    返回calendarUriBase;
}

当我运行这个code,我正在抛出:IllegalArgumentException

  java.lang.IllegalArgumentException异常:未知的URL内容://com.android.calendar/

我认为错误是因为 getCalendarUriBase()方法。我搜索了很多其他方式,但到目前为止,大部分的开发者按照上述code段,因为这会为每一个Android的版本。如何解决呢?


解决方案

您错过了日历当您尝试执行此行

 光标光标= getContentResolver()查询(
    Uri.parse(getCalendarUriBase(本​​))/ *< - 未知的URL内容* /
    新的String [] {CALENDAR_ID,显示名称},
    NULL,NULL,NULL);

造成抛出:IllegalArgumentException

只需添加日历 getCalendarUriBase(本​​)

 光标光标= getContentResolver()查询(
    Uri.parse(getCalendarUriBase(本​​)+日历),
    新的String [] {_id,显示名},
    NULL,NULL,NULL);


  

注意:


  
  

      
  • 使用 _id 如果您从日历表查询来获取日历ID。 CALENDAR_ID 被定义为其他表的外键(例如事件)。

  •   
  • 使用显示名为姜饼以下。对于冰淇淋三明治及以上,使用 calendar_displayName

  •   


当添加事件,你需要包括:日历ID,开始日期,结束日期(或复发的规则),以及事件的时区。你已经囊括了所有,但时区。尝试添加

  values​​.put(eventTimezone,TimeZone.getDefault()的getID());

在插入事件。

I am working on an Android application. I have to add events to native Android calendar. So I tried the following code:

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, 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, null, null, 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, null, null, null);
        } catch (Exception e) {
        }
        if (managedCursor != null) {
            calendarUriBase = "content://com.android.calendar/";
        }
    }
    return calendarUriBase;
}

When I run this code, I am getting the IllegalArgumentException.

java.lang.IllegalArgumentException: Unknown URL content://com.android.calendar/

I think the error is because of getCalendarUriBase() method. I searched a lot for other ways, but so far most of the developers following the above code snippet because this will work for every Android versions. How to fix it?

解决方案

You missed calendars when you try to execute this line

Cursor cursor = getContentResolver().query(
    Uri.parse(getCalendarUriBase(this)), /* <-- unknown URL content */
    new String[] { "calendar_id", "displayname" },
    null,null,null);

resulting in IllegalArgumentException.

Just add the calendars after getCalendarUriBase(this).

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

Note:

  • Use _id to get the calendar ID if you query from calendars table. calendar_id is defined as a foreign key in other tables (e.g. events).
  • Use displayName for GingerBread or below. For Ice Cream Sandwich and above, use calendar_displayName.


When adding an event, you need to include: calendar ID, start date, end date (or recurrence rule), and event timezone. You have included all but the timezone. Try adding

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

before inserting the event.

这篇关于添加事件,本地日历不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆