Chaine
Chaine

Reputation: 1400

How to save value from System.currentTimeMillis() in android studio

I have this code:

public class MainActivity extends AppCompatActivity implements SensorEventListener {

    long start_time;
    int record_state;

    @Override
    public void onSensorChanged(SensorEvent event) {
    Long time = System.currentTimeMillis();

    if (record_state == 1)
    {
        start_time = time;
        record_state = 0;
    }

    if(Ax.size() == N_SAMPLES && Ay.size() == N_SAMPLES && Az.size() == N_SAMPLES) //assuming this gets executed 
    {
        Toast.makeText(MainActivity.this, "Size of Ax: " + Integer.toString(Ax.size()) +
                    "\nSize of Ay: " + Integer.toString(Ay.size()) +
                    "\nSize of Az: " + Integer.toString(Az.size()) + "\n" + Long.toString((time)) + "\n" + Long.toString((start_time)) + "\nrecord state: " + Integer.toString((record_state)), Toast.LENGTH_LONG).show();
        }
    }
}

But it appears that time and start_time always have the same value. I want start_time to record the time at the very beginning (or freeze the value of time at one instant) only. How can I do this? What is wrong with this code?

Upvotes: 0

Views: 1568

Answers (3)

Barns
Barns

Reputation: 4848

If the code below does not work as you expected it to, then I suspect, that you have an issue with when and how "record_state" is being set somewhere in your code.

Local values with class wide scope:

int record_state = 1;
int iterations = 0;
long start_time;

When your onSensorChanged triggers call checkTimeDiff

private void checkTimeDiff(){
    iterations++;

    long time = SystemClock.elapsedRealtime();
    if (record_state == 1)
    {
        start_time = SystemClock.elapsedRealtime();
        record_state = 0;
    }

    long diff = time - start_time;
    Log.e("My Timer", "Time difference = " + diff + " number of iterations = " + iterations);

    if (diff >= 4000)
    {
        record_start = 1;
        Toast.makeText(MainActivity.this, Long.toString(time) + "\n" + Long.toString(start_time), Toast.LENGTH_SHORT).show();
    }
}

It appears as if you are using "record_state" as a digital flag. In that case a boolean would be more elegant. But I will use your code as much as possible.

Upvotes: 1

Xenolion
Xenolion

Reputation: 12735

Because this code is going to be used in onSensorChanged (referring to your comments) then the solution should be these variables should be declared and used throughout your class and not in the single method and the problem will disappear so please declare this outside any method as local variables:

int record_state = 1;
Long start_time;

Code and logic improvements

  • Use long instead of Long

  • Use >= instead of == because its too hard to get it exactly equal!

Upvotes: 0

eimmer
eimmer

Reputation: 1709

Use lowercase 'long'. You are using objects currently and pointing them both to the same reference, so when you change the value of one it affects the other.

Upvotes: 0

Related Questions