Android:计算设置提醒的时间

时间:2013-02-26 05:27:54

标签: android android-alarms android-calendar

我正在开发一个应用程序。我在设备日历中设置提醒。我通过调用for()循环中的addReminder方法在我的应用程序中设置多个提醒。设置提醒的代码如下。

private void addReminder(int statrYear, int startMonth, int startDay, int startHour, int startMinut, String title){
    // Convert start of begin time of reminder in milliseconds.
    Calendar beginTime = Calendar.getInstance();
    beginTime.set(statrYear, startMonth, startDay, startHour, startMinut);
    long startMillis = beginTime.getTimeInMillis();

    // String to access default google calendar of device for Event setting.
    String eventUriString = "content://com.android.calendar/events";

    // Creation of Event.
    ContentValues eventValues = new ContentValues();
    // Set calendar as 1 for default calendar.
    eventValues.put(Events.CALENDAR_ID, 1);
    // Set title as user define.
    eventValues.put(Events.TITLE, title);
    // Set description as user define.
    eventValues.put(Events.DESCRIPTION, "MYApp");
    // Set location as user define.
    eventValues.put(Events.EVENT_TIMEZONE, "India");
    // Set start time as system time or time converted in milliseconds.
    eventValues.put(Events.DTSTART, startMillis);
    // Set status of event as 1.
    eventValues.put("eventStatus", 1);
    // Set visibility of event as 3 (public).
    eventValues.put("visibility", 3);
    // Set transparency as 0. No other app seen through reminder.
    eventValues.put("transparency", 0);
    // Set alarm as 1. Ringing.
    eventValues.put(Events.HAS_ALARM, 1);

    // Set Event in calendar.
    Uri eventUri = getContentResolver().insert(Uri.parse(eventUriString), eventValues);
    // Getting ID of event in Long.
    long eventID = Long.parseLong(eventUri.getLastPathSegment());

    /***************** Event: Reminder(with alert) Adding reminder to event *******************/
    // String to access default google calendar of device for reminder setting.
    String reminderUriString = "content://com.android.calendar/reminders";      
    ContentValues reminderValues = new ContentValues();

    // Set reminder on Event ID.
    reminderValues.put("event_id", eventID);
    // Set reminder minute before.
    reminderValues.put("minutes", 1);
    // Set method of reminder
    reminderValues.put("method", 1);        

    @SuppressWarnings("unused")
    //Setting reminder in calendar on Event.
    Uri reminderUri = getContentResolver().insert(Uri.parse(reminderUriString), reminderValues);
}

调用上述方法的for循环是:

for(int i=0; i<numberOfReminder; i++){
    addReminder(statrYear, startMonth, startDay, startHour, startMinut, title);
}

现在我需要:

我想以秒为单位计算在日历中设置一个提醒的时间。设置所有提醒所需的总时间,提醒的总数在运行时提供。

那么我应该怎么做才能计算出以秒或毫秒为单位设置一个和多个提醒的时间。

1 个答案:

答案 0 :(得分:0)

有几种解决方案:

  1. 使用Log信息。
  2. 测量时间如下:

    long start = System.currentTimeMillis();

    提醒设置

    long end = System.currentTimeMillis(); //或start = start - System.currentTimeMillis();

    然后才结束 - 开始=? *请注意,这是以毫秒为单位,因此您需要将其除以1000以获得秒数。

  3. 干杯