Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3618122
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:42:40+00:00 2026-05-18T22:42:40+00:00

I am tring to record the wake time and sleep time from a dialog

  • 0

I am tring to record the wake time and sleep time from a dialog picker to a text file like this, but the call to the method commitToFile2 doesn’t append the text file “savedData.txt.”

I know this code is very very dirty. I’m new to Java so any other suggestions would be greatly appreciated.

package com.buttinyourface;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.AdapterView.OnItemClickListener;

public class SettingsActivity extends Activity {

    public int wakeHour;
    public int wakeMinute;
    public int sleepHour;
    public int sleepMinute;
    public String sleepHourText = "No Data";
    public String sleepMinuteText = "No Data";
    public String outputTime = "No Data";
    public String wakeHourText = "No Data";
    private ListView lv;
    Dialog newDialogBox;
    Context appContext;
    protected Context context;
    static final private int WAKE_TIME = 0;
    static final private int SLEEP_TIME = 1;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
        final String[] settingsList = getResources().getStringArray(
                R.array.settingsStringArray);
        lv = (ListView) findViewById(R.id.list);
        TextView wakeHourTextView = (TextView) findViewById(R.id.TextView01);
        lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                int settingsPosition = position;
                if (settingsPosition == 0) {
                    showDialog(WAKE_TIME);
                    wakeHourText = Integer.toString(wakeHour);
                }
                if (settingsPosition == 1) {
                    showDialog(SLEEP_TIME);
                }
            }
        });
        lv.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, settingsList));
        wakeHourTextView.setText(outputTime);
    }

    public void onPrepareDialog(int id, Dialog dialog) {
        switch (id) {
        case WAKE_TIME:
            break;
        case SLEEP_TIME:
            break;
        }
    }

    @Override
    public Dialog onCreateDialog(int id) {
        switch (id) {
        case WAKE_TIME:
            return new TimePickerDialog(this, WakeTimeSetListener, wakeHour,
                    wakeMinute, false);
        case SLEEP_TIME:
            return new TimePickerDialog(this, SleepTimeSetListener, sleepHour,
                    sleepMinute, false);
        }
        return null;
    }

    private TimePickerDialog.OnTimeSetListener WakeTimeSetListener =
                          new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            wakeHour = hourOfDay;
            wakeMinute = minute;
            String wakeHourText = Integer.toString(hourOfDay);
            String wakeMinuteText = Integer.toString(minute);
            String preftime = hourOfDay + ":" + minute;
            SimpleDateFormat df = new SimpleDateFormat("HH:mm");
            SimpleDateFormat dfOut = new SimpleDateFormat("hh:mma");
            Date date = null;
            try {
                date = df.parse(preftime);
            } catch (ParseException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            String outputWakeTime = dfOut.format(date);
            try {
                commitToFile(wakeHourText, wakeMinuteText, outputWakeTime);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    private TimePickerDialog.OnTimeSetListener SleepTimeSetListener =
                          new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            sleepHour = hourOfDay;
            sleepMinute = minute;
            String sleepHourText = Integer.toString(hourOfDay);
            String sleepMinuteText = Integer.toString(minute);
            String preftime = hourOfDay + ":" + minute;
            SimpleDateFormat df = new SimpleDateFormat("HH:mm");
            SimpleDateFormat dfOut = new SimpleDateFormat("hh:mma");
            Date date = null;
            try {
                date = df.parse(preftime);
            } catch (ParseException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            String sleepOutputTime = dfOut.format(date);
            try {
                commitToFile2(sleepHourText, sleepMinuteText, sleepOutputTime);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };

    private void commitToFile(String wakeHourText, String wakeMinuteText,
            String outputWakeTime) throws IOException {
        final String entryString = new String("wakeHour=" + wakeHourText
                + ";wakeMinute=" + wakeMinuteText + ";wakeTime="
                + outputWakeTime + ";");
        FileOutputStream fOut = openFileOutput("savedData.txt",
                MODE_WORLD_READABLE);
        OutputStreamWriter osw = new OutputStreamWriter(fOut);
        osw.write(entryString);
        osw.flush();
        osw.close();
    }

    private void commitToFile2(String sleepHourText, String sleepMinuteText,
            String sleepOutputTime) throws IOException {
        final String entryString = new String("sleepHour=" + sleepHourText
                + ";sleepMinute=" + sleepMinuteText + ";sleepTime="
                + sleepOutputTime + ";");
        FileOutputStream fOut = openFileOutput("savedData.txt",
                MODE_WORLD_READABLE);
        OutputStreamWriter osw = new OutputStreamWriter(fOut);
        osw.write(entryString);
        osw.flush();
        osw.close();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T22:42:41+00:00Added an answer on May 18, 2026 at 10:42 pm

    I figured it out….I had to change the line

    FileOutputStream fOut = openFileOutput("savedData.txt", MODE_WORLD_READABLE);
    

    to

    FileOutputStream fOut = openFileOutput("savedData.txt",  MODE_APPEND);
    

    After that I was able to append the text file without overwriting the data that was already inside the text file. Thanks for your assistance guys. I guess going to the 4th page on google IS useful sometimes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying out the record outgoing call using mic written this code but
I'm tring to grab all fields from the latest Cash record, and then all
I've tried to overcome this for a while. I'm trying to record sound, but
I am tring update a record and want to exclude one column from the
Like in this question I am trying to record the exact position when parsing
I am trying to record using the iphones microphone: This is my code: NSArray
I'm trying to record audio this.recorder = new android.media.MediaRecorder(); this.recorder.setAudioSource(android.media.MediaRecorder.AudioSource.MIC); this.recorder.setOutputFormat(android.media.MediaRecorder.OutputFormat.DEFAULT); this.recorder.setAudioEncoder(android.media.MediaRecorder.AudioEncoder.DEFAULT); this.recorder.setOutputFile(pruebaAudioRecorder.mp4); **this.recorder.prepare();**
I am trying to record whenever a user clicks on Facebook Like button. Facebook
iam trying to record sound from an activex WMP control playing an internet radio
I'm trying to add record locking to a site. This isn't database locking where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.