2012-12-04 45 views
20

我想在android 4 +中以编程方式(直接)添加日历事件。这是否可以在仿真器上测试?我不拥有Android手机。一些示例代码将不胜感激。我阅读Android开发人员的日历提供程序,但我很困惑。我如何将事件添加到用户的默认日历?我不需要同步。如何在Android 4中将日历事件添加到默认日历中,默默无声地使用Intent?

编辑:我不想发起添加意图的事件。相反,我想从代码中完全添加它们,而不是启动另一个活动。我需要能够在模拟器上测试事件将被添加到设备默认用户的主日历中。如何设置模拟器来查看用户的默认日历?

+0

的可能的复制[如何添加日历事件的Android?](http://stackoverflow.com/questions/3721963/how-to-add-calendar得到压延ID -events-in-android) – jww

+1

@jww他想静静地做,不同于其他问题 – vitormm

回答

32

Here是什么,我最终做了它的工作例如:

ContentResolver cr = ctx.getContentResolver(); 
ContentValues values = new ContentValues(); 

values.put(CalendarContract.Events.DTSTART, dtstart); 
values.put(CalendarContract.Events.TITLE, title); 
values.put(CalendarContract.Events.DESCRIPTION, comment); 

TimeZone timeZone = TimeZone.getDefault(); 
values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID()); 

// Default calendar 
values.put(CalendarContract.Events.CALENDAR_ID, 1); 

values.put(CalendarContract.Events.RRULE, "FREQ=DAILY;UNTIL=" 
     + dtUntill); 
// Set Period for 1 Hour 
values.put(CalendarContract.Events.DURATION, "+P1H"); 

values.put(CalendarContract.Events.HAS_ALARM, 1); 

// Insert event to calendar 
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values); 

其中dtuntil

SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd"); 
Calendar dt = Calendar.getInstance(); 

// Where untilDate is a date instance of your choice, for example 30/01/2012 
dt.setTime(untilDate); 

// If you want the event until 30/01/2012, you must add one day from our day because UNTIL in RRule sets events before the last day 
dt.add(Calendar.DATE, 1); 
String dtUntill = yyyyMMdd.format(dt.getTime()); 

Ref:Recurrence Rule

+3

不应将格式设置为“yyyyMMdd”而不是“yyyymmdd”? “m”表示根据这个分钟数:http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html – silmeth

+0

闹钟对我来说只适用于这种格式:'values.put( CalendarContract.Events.HAS_ALARM,false);' – keybee

+0

看看这个问题:http://stackoverflow.com/questions/28871921/add-weekly-event-to-calendar – Sun

9

我相信你正在寻找的部分是Using an intent to insert an event。在本节中,它描述了如何为要添加的事件创建一个意图,然后模拟器上的默认压光机程序将响应并添加它。如果您确实希望看到它收到正确的信息,您可能需要设置一个虚拟配置文件,以便日历程序启动。


代码从Android Dev Site

Calendar beginTime = Calendar.getInstance(); 
beginTime.set(2012, 0, 19, 7, 30); 
Calendar endTime = Calendar.getInstance(); 
endTime.set(2012, 0, 19, 8, 30); 
Intent intent = new Intent(Intent.ACTION_INSERT) 
    .setData(Events.CONTENT_URI) 
    .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis()) 
    .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis()) 
    .putExtra(Events.TITLE, "Yoga") 
    .putExtra(Events.DESCRIPTION, "Group class") 
    .putExtra(Events.EVENT_LOCATION, "The gym") 
    .putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY) 
    .putExtra(Intent.EXTRA_EMAIL, "[email protected],[email protected]"); 
startActivity(intent); 
+1

不,我想以编程方式添加这个事件,像http://stackoverflow.com/questions/7859005/how-to-阅读并编辑android-calendar-events-using-the-new-android-4-0-ice-cream – oikonomopo

+0

在那个链接中他们也显示了上面的这个方法。我如何描述不是程序化的?你的意思是你不想使用意图?你想默默地添加事件吗? – Velocitas

+1

是的,我想悄悄地将事件添加到默认日历。 – oikonomopo

2

使用此代码,您可以以编程方式将事件添加到设备日历。我已经在棉花糖测试过了,对我来说它工作的很好。

private void addToDeviceCalendar(String startDate,String endDate, String title,String description, String location) { 

     String stDate = startDate; 
     String enDate = endDate; 

     GregorianCalendar calDate = new GregorianCalendar(); 
     //GregorianCalendar calEndDate = new GregorianCalendar(); 

     SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); 
     SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy,MM,dd,HH,mm"); 
     Date date,edate; 
     try { 
      date = originalFormat.parse(startDate); 
      stDate=targetFormat.format(date); 

     } catch (ParseException ex) {} 

     long startMillis = 0; 
     long endMillis = 0; 
     String dates[] = stDate.split(","); 

     SD_YeaR = dates[0]; 
     SD_MontH = dates[1]; 
     SD_DaY = dates[2]; 
     SD_HouR = dates[3]; 
     SD_MinutE = dates[4]; 


     /*Log.e("YeaR ", SD_YeaR); 
     Log.e("MontH ",SD_MontH); 
     Log.e("DaY ", SD_DaY); 
     Log.e(" HouR", SD_HouR); 
     Log.e("MinutE ", SD_MinutE);*/ 

     calDate.set(Integer.parseInt(SD_YeaR), Integer.parseInt(SD_MontH)-1, Integer.parseInt(SD_DaY), Integer.parseInt(SD_HouR), Integer.parseInt(SD_MinutE)); 
     startMillis = calDate.getTimeInMillis(); 
/* 
     try { 
      edate = originalFormat.parse(endDate); 
      enDate=targetFormat.format(edate); 

     } catch (ParseException ex) {} 


     String end_dates[] = endDate.split(","); 

     String ED_YeaR = end_dates[0]; 
     String ED_MontH = end_dates[1]; 
     String ED_DaY = end_dates[2]; 

     String ED_HouR = end_dates[3]; 
     String ED_MinutE = end_dates[4]; 


     calEndDate.set(Integer.parseInt(ED_YeaR), Integer.parseInt(ED_MontH)-1, Integer.parseInt(ED_DaY), Integer.parseInt(ED_HouR), Integer.parseInt(ED_MinutE)); 
     endMillis = calEndDate.getTimeInMillis();*/ 

     try { 
      ContentResolver cr = getActivity().getContentResolver(); 
      ContentValues values = new ContentValues(); 
      values.put(CalendarContract.Events.DTSTART, startMillis); 
      values.put(CalendarContract.Events.DTEND, calDate.getTimeInMillis() + 60 * 60 * 1000); 
      values.put(CalendarContract.Events.TITLE, title); 
      values.put(CalendarContract.Events.DESCRIPTION, description); 
      values.put(CalendarContract.Events.EVENT_LOCATION,location); 
      values.put(CalendarContract.Events.HAS_ALARM,1); 
      values.put(CalendarContract.Events.CALENDAR_ID, 1); 
      values.put(CalendarContract.Events.EVENT_TIMEZONE, Calendar.getInstance() 
        .getTimeZone().getID()); 
      System.out.println(Calendar.getInstance().getTimeZone().getID()); 
      if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) { 

       return; 
      } 
      Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values); 

      long eventId = Long.parseLong(uri.getLastPathSegment()); 
      Log.d("Ketan_Event_Id", String.valueOf(eventId)); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
+1

请添加一些观点,而不是仅仅粘贴代码。 – surajsn

+0

@Ketan Ramani:没有在Marshmellow设备上工作,但没有错误。 – Aniruddha

+0

我认为这是代码中缺少一些部分... – exequielc

0

同意以上所有答案,但导入是日历ID。你不能使用1作为三星手机使用1为他们的日历(S规划师)。所以日历ID是您希望事件的电子邮件的标识。您可以通过下面的代码为特定事件

int calenderId=-1; 
     String calenderEmaillAddress="[email protected]"; 
     String[] projection = new String[]{ 
       CalendarContract.Calendars._ID, 
       CalendarContract.Calendars.ACCOUNT_NAME}; 
     ContentResolver cr = activity.getContentResolver(); 
     Cursor cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), projection, 
       CalendarContract.Calendars.ACCOUNT_NAME + "=? and (" + 
         CalendarContract.Calendars.NAME + "=? or " + 
         CalendarContract.Calendars.CALENDAR_DISPLAY_NAME + "=?)", 
       new String[]{calenderEmaillAddress, calenderEmaillAddress, 
         calenderEmaillAddress}, null); 

     if (cursor.moveToFirst()) { 

      if (cursor.getString(1).equals(calenderEmaillAddress)) 
       calenderId=cursor.getInt(0); //youre calender id to be insered in above 2 answer 


     } 
相关问题