Hello I'm very new to android programming. I'd like to calculate the sleep cycle of a person in Android. I found a java code from the net but I don't know how to apply it in Android.

Here's the code I found:

  /**
   * Get times to wake. We add 14 minutes to fall asleep, and calculate the
   * sleep cycles.
   * 
   * @return list of times when to get up
   */
  public static Set<Date> getWakingTimes() {
    return getWakingTime(new Date());
  }

  /**
   * Get times to wake. We add 14 minutes to fall asleep, and calculate the
   * sleep cycles.
   * 
   * @param sleepTime
   *            bed time
   * 
   * @return list of times when to get up
   */
  public static Set<Date> getWakingTime(Date sleepTime) {
    Calendar fallAsleep = Calendar.getInstance();
    fallAsleep.setTime(sleepTime);
    fallAsleep.add(Calendar.MINUTE, 14);
    Set<Date> result = new TreeSet<Date>();
    for (int i = 1; i <= 6; i++) {
      fallAsleep.add(Calendar.MINUTE, 90);
      result.add(fallAsleep.getTime());
    }
    return result;
  }

  /**
   * Get sleeping times corresponding to a local time
   * 
   * @param wakingTime
   *            time to wake up !
   * @return list of times one should go to bed to
   */
  public static Set<Date> getSleepingTimes(Date wakingTime) {

    Set<Date> result = new TreeSet<Date>();
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(wakingTime);
    calendar.add(Calendar.MINUTE, -14);
    calendar.add(Calendar.MINUTE, -(2 * 90));
    for (int i = 3; i <= 6; i++) {
      calendar.add(Calendar.MINUTE, -90);
      result.add(calendar.getTime());
    }
    return result;

  } // end of getSleepingTimes method


  private void resetSpinnerValues()
  {
      spHour.setSelection(0);
      spMinute.setSelection(0);
      spTime.setSelection(0);

  } // end of resetSpinnerValues method

I'd like to try this: Click Here(null)
If anyone knows please help. Thanks.

Recommended Answers

All 6 Replies

What is exactly an issue? You do not know how to build Android UI?

I know how to build UI, my issue is how to put those methods into the android code and make it displayed in a textview.

Well then provide what you have because otherwise I can only tell you that on some action call on your methods, get results and display or store it somewhere.

You may consider rather use TimePicker as you do not need to mess around with these arrays to populate it (Pickers tutorial).
Secondly what that screen is supposed to achieve? Just let user set values and run calculation? If so then as I said attach listener to button, get data from spinner (drop down option), call the methof for calculation and forward your time data to it.

Hello I'm trying to do it like this:

    public static Set<Date> getWakingTime( Date sleepTime ) {

    Calendar fallAsleep = Calendar.getInstance();
    fallAsleep.setTime( sleepTime );
    fallAsleep.add( Calendar.MINUTE, 14 );
    Set<Date> result = new TreeSet<Date>();
    for ( int i = 1; i <= 6; i++ ) {
      fallAsleep.add( Calendar.MINUTE, 90 );
      result.add( fallAsleep.getTime() );
    }

     return result;

    answer.setText(result);

    }

But it's giving some errors

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.