添加日历事件,而无需打开日历 [英] add calendar event without opening calendar

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

问题描述

在我的应用程序,用户可以建立3个提醒任务,但每次我preSS它开辟了日历应用中的设置提醒按钮。有什么办法来设置日历事件,而无需打开默认的日历应用程序?我只是想而不启动日历活动添加事件。

这是我的code貌似现在:

 日历BEGINTIME = Calendar.getInstance();
beginTime.set(2013年,Calendar.MAY,10,3,00);
startMillis = beginTime.getTimeInMillis();
日历ENDTIME = Calendar.getInstance();
endTime.set(2012,Calendar.MAY,10,4,00);
endMillis = endTime.getTimeInMillis();意向意图=新意图(Intent.ACTION_INSERT);
intent.setType(vnd.android.cursor.item /事件);
intent.putExtra(Events.TITLE,测试的Andr​​oid);
intent.putExtra(Events.EVENT_LOCATION,测试位置);
intent.putExtra(Events.DESCRIPTION,考试说明的例子);intent.putExtra(Events.DTSTART,startMillis);
intent.putExtra(Events.DTEND,endMillis);
intent.putExtra(Events.ALL_DAY,FALSE);
intent.putExtra(Events.EVENT_END_TIMEZONE,欧洲/伦敦);
intent.putExtra(Events.ACCESS_LEVEL,Events.ACCESS_PRIVATE);
intent.putExtra(Events.AVAILABILITY,Events.AVAILABILITY_BUSY);


解决方案

这将是这个样子:

 最后ContentValues​​事件=新ContentValues​​();
    event.put(Events.CALENDAR_ID,1);    event.put(Events.TITLE,职称);
    event.put(Events.DESCRIPTION,说明);
    event.put(Events.EVENT_LOCATION,位置);    event.put(Events.DTSTART,startTimeMillis);
    event.put(Events.DTEND,endTimeMillis);
    event.put(Events.ALL_DAY,0); // 0表示假,1为真
    event.put(Events.HAS_ALARM,1); // 0表示假,1为真    字符串的timeZone = TimeZone.getDefault()的getID()。
    event.put(Events.EVENT_TIMEZONE,的timeZone);    乌里基本URI;
    如果(Build.VERSION.SDK_INT> = 8){
        基本URI = Uri.parse(内容://com.android.calendar/events);
    }其他{
        基本URI = Uri.parse(内容://日历/事件);
    }    。context.getContentResolver()插入(基本URI,事件);

In my app, the user can set up to 3 reminders for a task, but every time I press the "set reminder" button it opens up the calendar app. Is there any way to set the calendar events without opening the default calendar app? I just want to add an event without starting the calendar activity.

This is what my code looks like now:

Calendar beginTime = Calendar.getInstance();
beginTime.set(2013, Calendar.MAY, 10, 3, 00);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2012, Calendar.MAY, 10, 4, 00);
endMillis = endTime.getTimeInMillis();

Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra(Events.TITLE, "Test Android");
intent.putExtra(Events.EVENT_LOCATION, "Test Location");
intent.putExtra(Events.DESCRIPTION, "Test Description Examples");

intent.putExtra(Events.DTSTART, startMillis);
intent.putExtra(Events.DTEND, endMillis);
intent.putExtra(Events.ALL_DAY, false);
intent.putExtra(Events.EVENT_END_TIMEZONE, "Europe/London");


intent.putExtra(Events.ACCESS_LEVEL, Events.ACCESS_PRIVATE);
intent.putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY);

解决方案

This will look something like this:

    final ContentValues event = new ContentValues();
    event.put(Events.CALENDAR_ID, 1);

    event.put(Events.TITLE, title);
    event.put(Events.DESCRIPTION, description);
    event.put(Events.EVENT_LOCATION, location);

    event.put(Events.DTSTART, startTimeMillis);
    event.put(Events.DTEND, endTimeMillis);
    event.put(Events.ALL_DAY, 0);   // 0 for false, 1 for true
    event.put(Events.HAS_ALARM, 1); // 0 for false, 1 for true

    String timeZone = TimeZone.getDefault().getID();
    event.put(Events.EVENT_TIMEZONE, timeZone);

    Uri baseUri;
    if (Build.VERSION.SDK_INT >= 8) {
        baseUri = Uri.parse("content://com.android.calendar/events");
    } else {
        baseUri = Uri.parse("content://calendar/events");
    }

    context.getContentResolver().insert(baseUri, event);

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

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