如何在android模拟器中创建日历事件。

时间:2014-07-21 18:55:57

标签: android events calendar android-calendar android-syncadapter

我正在尝试将日历活动添加到我的自定义帐户中。

我已使用以下代码

成功创建了日历活动
    private static long addCalendar(Account account) throws RemoteException, OperationApplicationException {


Uri calenderUri = Calendars.CONTENT_URI.buildUpon().appendQueryParameter(Calendars.ACCOUNT_NAME, account.name).appendQueryParameter(
Calendars.ACCOUNT_TYPE, account.type).build();
Cursor c1 = mContentResolver.query(calenderUri, new String[] { BaseColumns._ID }, null,    null, null);
if (c1.moveToNext()) {
return c1.getLong(0);
} else {
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();

 ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(Calendars.CONTENT_URI.buildUpon()
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(Calendars.ACCOUNT_NAME, account.name)
.appendQueryParameter(Calendars.ACCOUNT_TYPE, account.type)

.build()
);
builder.withValue(Calendars.ACCOUNT_NAME, account.name);
builder.withValue(Calendars.ACCOUNT_TYPE, account.type);
builder.withValue(Calendars.NAME, "com.example.karthi");
builder.withValue(Calendars.CALENDAR_DISPLAY_NAME, "com.example.karthi");
builder.withValue(Calendars.CALENDAR_COLOR, 0xD51007);
builder.withValue(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_READ);
builder.withValue(Calendars.OWNER_ACCOUNT, account.name);
builder.withValue(Calendars.VISIBLE, 1);
builder.withValue(Calendars.SYNC_EVENTS, 1);
operationList.add(builder.build());
try {
mContentResolver.applyBatch(CalendarContract.AUTHORITY, operationList);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
return addCalendar(account);
}
}
@SuppressLint("SimpleDateFormat")
public static void addEvent(Long id,Account account,String sid,String eventName,String s_date,String duration,String responsible) throws RemoteException, OperationApplicationException, ParseException{




        ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
        Date begin_date = format.parse(String.valueOf(s_date));
       long beginDate = begin_date.getTime();
        long endDate = beginDate + (Double.valueOf(duration).intValue() * HOUR);

     //Insert a new event

            Log.d("add event"+calendarID,"area");
            Log.d("event area","calendar id="+id);
            TimeZone timeZone = TimeZone.getDefault();

            ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(Events.CONTENT_URI.buildUpon()
                    .appendQueryParameter(Calendars.ACCOUNT_NAME, account.name)
                    .appendQueryParameter(Calendars.ACCOUNT_TYPE, account.type)
                    .appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
                    .build()
                    ); 
            builder.withValue(CalendarContract.Events.CALENDAR_ID, id);
            builder.withValue(CalendarContract.Events.DTSTART, beginDate);
            builder.withValue(CalendarContract.Events.DTEND, endDate);

            builder.withValue(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
            builder.withValue(CalendarContract.Events.TITLE, eventName);
            operationList.add(builder.build());
            mContentResolver.applyBatch(CalendarContract.AUTHORITY, operationList);

               ContentProviderResult[] res = mContentResolver.applyBatch(CalendarContract.AUTHORITY, operationList);

                Uri myContactUri = res[0].uri;
                int lastSlash = myContactUri.toString().lastIndexOf("/");
                int length = myContactUri.toString().length();
                String event_id =(String) myContactUri.toString().subSequence(lastSlash+1, length);

                s_date=String.valueOf(beginDate);
               String  e_date=String.valueOf(endDate);
            DBAdapter.addEventsData(sid, eventName, s_date, e_date, responsible, event_id);


}

我的帐户显示在设置 - &gt;要显示的日历区域...... 上面的代码也为我的自定义帐户创建了一个事件..这段代码工作正常。

但我想在android模拟器中添加一个事件,它会给出以下错误, enter image description here

我也尝试添加帐户选项。如果我按添加帐户,则允许创建我的自定义帐户。

再次尝试从android模拟器创建一个事件。再次显示没有日历消息..任何帮助或命令欢迎...

注意:我不想以编程方式添加事件,它已经从上面的代码中创建

1 个答案:

答案 0 :(得分:1)

最后我找到了解决方案。这是我的错。我仅提供访问级别 READ

builder.withValue(Calendars.CALENDAR_ACCESS_LEVEL,Calendars.CAL_ACCESS_READ);

将此行更改为

builder.withValue(Calendars.CALENDAR_ACCESS_LEVEL,Calendars.CAL_ACCESS_OWNER);

现在工作正常。