当前位置: 动力学知识库 > 问答 > 编程问答 >

time - Android: Updating int ever second

问题描述:

I am creating a text file with data related to my App's game.

I want to show the score at each second in the game.

How can I ensure that my int for seconds is updated, starting at zero

Example of output wanted:

Seconds Score

0 3

1 9

2 16

3 20

.....etc

Current output (seconds always 0):

Seconds Score

0 3

0 9

0 16

0 20

.....etc

Current code:

int seconds=0;

//creating header in the txt file Note: Blanked out as it is generating every second

writeToFileEEGPower(order("Seconds")+order("highAlpha")+order("lowAlpha")+order("highBeta")+order("LowBeta")+

order("lowGamma")+order("midGamma")+order("Delta")+order("Theta")+ "\n");

//creating the string to be written to file

String line = order(seconds+"")+order(eegPower.highAlpha+"")+order(eegPower.lowAlpha+"")+order(eegPower.highBeta+"")+

order(eegPower.lowBeta+"")+order(eegPower.midGamma+"")+order(eegPower.delta+"")+order(eegPower.theta+"")+ "\n";

//write the string to file

writeToFileEEGPower(line);

seconds++;

网友答案:

I think you should rather use a sqlite table for this. Greendao is a good tool for managing those tables. You can save the information every second to a table. When the game is finished, you have a full list of the score for each second of the game. In your code example the "second" variable is set to 0 each time before you write a new line. I think that is the problem.

网友答案:

Using TimerTask or Handler with postDelayed() method can do the trick.

网友答案:

You can use a handler to update your text file every second:

public class MyActivity extends Activity {

    private android.os.Handler mHandler = new android.os.Handler();
    private Runnable textFileLogger;
    private int seconds = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        //Create header for text file
        writeToFileEEGPower(order("Seconds") + order("highAlpha") + order("lowAlpha") + order("highBeta") + order("LowBeta") +
                order("lowGamma") + order("midGamma") + order("Delta") + order("Theta") + "\n");

        textFileLogger = new Runnable() {
            @Override
            public void run() {
                seconds++;
                String line = order(seconds + "") + order(eegPower.highAlpha + "") + order(eegPower.lowAlpha + "") + order(eegPower.highBeta + "") +
                        order(eegPower.lowBeta + "") + order(eegPower.midGamma + "") + order(eegPower.delta + "") + order(eegPower.theta + "") + "\n";
                //write the string to file
                writeToFileEEGPower(line);
                //Repeats the logging after 1 second
                mHandler.postDelayed(this, 1000);
            }
        };

        //Starts the logging after 1 second
        mHandler.postDelayed(textFileLogger, 1000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //To stop the logging:
        mHandler.removeCallbacks(textFileLogger);
    }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: