2012-11-30 12 views
7

我有一個Android應用程序,下載一些服務的一些藥物信息。直到特定的一天沒有意圖在Android中創建帶有提醒的重複事件?

例如(白色FLUDEX輪2 24-02-2012),是指藥物名爲FLUDEX,白色和圓形,必須從今天直到24-01-2012每天給予2次。

現在我想經過藥物信息下載,與藥物信息添加重複事件日曆默默地/編程(無需用戶交互)。 所以,從今天直到24-01-2012每10點和晚上10點有一個提醒前10分鐘把他的藥。 我的應用程序將爲Android 2-4。 我怎麼能這樣做,我從我的搜索到目前爲止困惑。

第二個問題:我如何刪除從我的應用程序中創建的事件(以及它們的提醒),因此當我同步我的藥物治療以刪除所有先前的事件並基於從我接收到的新葯物治療產生新事件時服務?

+0

看看這個問題:http://stackoverflow.com/questions/28871921/add-weekly-event-to-calendar – Sun

回答

14
 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); 
     //for one 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 we add one day from our day because UNTIL in RRule sets events Before the last day want for event 
    dt.add(Calendar.DATE, 1); 
    String dtUntill = yyyymmdd.format(dt.getTime()); 

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

    // get the event ID that is the last element in the Uri 
    long eventID = Long.parseLong(uri.getLastPathSegment()); 

    // add 10 minute reminder for the event 
    ContentValues reminders = new ContentValues(); 
    reminders.put(Reminders.EVENT_ID, eventID); 
    reminders.put(Reminders.METHOD, Reminders.METHOD_ALERT); 
    reminders.put(Reminders.MINUTES, 10); 

    Uri uri = cr.insert(Reminders.CONTENT_URI, reminders); 
得到幫助

Ref:Recurrence Rule

+0

+1詳細答案 –

+0

這裏是什麼ctx? – Manikandan

+0

我們是否需要在清單中給予任何許可? – Manikandan

2

這裏是你想要的好Example

更新 有關日曆和實施提醒或其他的東西更多信息see this

您還可以從下面的代碼

Intent intent = new Intent(Intent.ACTION_EDIT); 
intent.setType("vnd.android.cursor.item/event"); 
intent.putExtra("beginTime", date); 
intent.putExtra("allDay", true); 
intent.putExtra("rrule", "FREQ=YEARLY"); //To set the repeat rule 
intent.putExtra("endTime", date); 
intent.putExtra("title", summary); 
+0

這創建/設置提醒/警報在某個date.But我想從現在開始重複N天。而且我還希望將事件插入日曆中,以便用戶可以看到它們。 – oikonomopo

+0

我們可以使用putExtra即duration intent.putExtra(「duration」,自定義時間); – Ali

相關問題