2013-05-10 64 views
1

在我的应用中,用户最多可以为任务设置3个提醒,但每次按下“设置提醒”按钮时,都会打开日历应用。有没有办法设置日历事件,而无需打开默认的日历应用程序?我只想添加一个事件而不启动日历活动。在没有打开日历的情况下添加日历活动

这是我的代码看起来像现在:

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); 

回答

8

这将是这个样子:

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); 
+0

首先是什么?PandoraUtil其次是你的代码的最后一行说activity.getCon .....我必须创建一个新的活动或这应该this.getContentResolver?谢谢....无法找到所谓的PandoraUtil ...它属于哪个软件包...或者它是否被弃用?顺便说一句我没有使用模拟器,我的设备是nexus 7与运行Android 4.2.2 – Amjad 2013-05-10 14:24:04

+1

对不起,意外包括一个私人实用程序。我已经更新了这个例子,使之更清晰一些。您可以使用任何上下文来调用getContentResolver()。如果这是从一个活动中调用的,那么最简单的方法就是将活动本身用于上下文。否则,您通常应该使用Application上下文(context.getApplicationContext())。 – dhaag23 2013-05-13 20:45:33

+0

@ dhaag23:我使用http://stackoverflow.com/questions/11586054/android-insert-calendar-intent-without-alarm-reminder代码做同样的事情。它在低于4.0(ICS)的情况下工作良好。但超过4.0它不工作。任何建议?我试了很长时间 – 2013-06-19 15:26:07