简体   繁体   English

在Android中没有Intent的特定日期创建带有提醒的重复事件?

[英]Create a repeating event with reminder until specific day without Intent in Android?

I have an android app that downloads with a service some drugs info. 我有一个Android应用程序下载服务一些药物信息。

For example (fludex white round 2 24-02-2012),means a drug named fludex ,white and round,must be given 2 times per day from today untill 24-01-2012. 例如(fludex white round 2 24-02-2012),意味着一种名为fludex,白色和圆形的药物,从今天起每天必须给予2次,直到24-01-2012。

Now i want after drug info downloading , to add repeated event with drug info to the calendar silently/programmatically(without user interaction). 现在我想在药物信息下载之后,以静默/编程方式向日历添加重复的药物信息事件(无需用户交互)。 So that from today untill 24-01-2012 every 10 am and 10pm to have a reminder 10 minutes before to take his drug. 所以,从今天开始,直到每天上午10点到晚上10点,才会在10分钟前提醒他服药。 My app will be for android 2-4. 我的应用程序将用于Android 2-4。 How can i do that,i'm confused from my searching so far. 我怎么能这样做,到目前为止我对我的搜索感到困惑。

Second question:How can i delete only the events(and their reminders) made from my application,so when i sync my drug therapy to delete all previous events and produce new events based on the new drug therapy i receive from my service? 第二个问题:我如何只删除从我的应用程序中制作的事件(及其提醒),所以当我同步我的药物治疗以删除所有先前的事件并根据我从我的服务收到的新药物治疗产生新事件?

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

where dtuntil is 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 参考: Recurrence Rule

Here is a good Example of what you want. 这是你想要的一个很好的例子

Update for more information about calender and implementing reminders or other stuff see this 有关日历和实施提醒或其他内容的更多信息的更新请参阅此内容

You can also get help from the following code 您还可以从以下代码获得帮助

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

To create a time bound reoccuring event over multiple days, we need to use CalendarContract.Events.RRULE . 要在多天内创建时间限制重新执行事件,我们需要使用CalendarContract.Events.RRULE The rule is combination frequency, count etc. 规则是组合频率,计数等。

Lets say we need to create an event occuring daily for 10 days in a specific time period of day: 假设我们需要在一天的特定时间段内创建每天发生10天的事件:

Intent(Intent.ACTION_INSERT)
            .setData(CalendarContract.Events.CONTENT_URI)
            .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,
                    beginCalendarTime.getTimeInMillis())
            .putExtra(CalendarContract.EXTRA_EVENT_END_TIME,
                    endCalendarTime.getTimeInMillis())
            .putExtra(CalendarContract.Events.TITLE, heading)
            .putExtra(CalendarContract.Events.DESCRIPTION, "To be added")
            .putExtra(CalendarContract.Events.EVENT_LOCATION, location)
            .putExtra(CalendarContract.Events.RRULE, "FREQ=DAILY;COUNT=10");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM