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: (null)
If anyone knows please help. Thanks.

Dani AI

Generated

Short answer: the compile/logic errors come from trying to both return a value and update the UI in the same method, and from passing a Set<Date> directly to TextView.setText(...). ’s suggestion to use a TimePicker is good — it makes getting the user’s hour/minute simpler.

Suggested flow

  1. Read hour/minute from your TimePicker (or from the spinners). Build a Calendar/Date that represents the bedtime.
  2. Call your existing getWakingTime(sleepDate) helper (keep that calculation in a non-UI utility method).
  3. Convert the returned Set<Date> into formatted strings.
  4. Put the joined string into your TextView (or populate a ListView/RecyclerView for nicer UX).

Example formatter (use in your Activity after the helper returns)

Set<Date> wakeTimes = SleepCalc.getWakingTime(sleepDate);
SimpleDateFormat fmt = new SimpleDateFormat("h:mm a", Locale.getDefault());
StringBuilder sb = new StringBuilder();
for (Date d : wakeTimes) {
  sb.append(fmt.format(d)).append('\n');
}
answerTextView.setText(sb.toString());

Troubleshooting / tips

  • Do not put answer.setText(...) after a return — code after return is unreachable. Either update the UI after the method returns, or have the method return the data and update the UI in the caller.
  • TextView.setText needs a CharSequence (String), not a Set<Date>. Format dates first.
  • Formatting: use SimpleDateFormat (or DateFormat.getTimeInstance() for locale-aware output).
  • UI thread: this calculation is cheap and can run on the UI thread; if you later add heavy work, run it off the UI thread and post results back with runOnUiThread or an executor.
  • For better UX, follow and prefer TimePicker instead of manual spinner arrays, and consider showing each wake time in a list so users can tap one to set an alarm.

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.