Search code examples
androiddelay

Android delay loop to collect data from the gyroscope


I have been creating this android application, and have learned a lot about java and android programming along the way. However, I just haven't been able to figure out how to work time delay loops. I looked at other stack posts and saw different methods, but was not able to successfully implement them. I will describe what I want the loop to do.

When I touch the screen of the android app, I want the app to speak ("Begin testing"). For the next 20 seconds, I want the app to call angmag every 50 milliseconds so I can get the angular velocity's magnitude and see if it's above a certain threshold. This function is called every 50 milliseconds for 20 seconds, until the app speaks "testing complete" and then finish testing. How would I go about implementing this delay function in the android app? I've commented where I want to the delay in the code.

package com.example.shivamgandhi.gyrosafe;

import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Balance_Test_Activity extends VoiceControl implements View.OnClickListener, SensorEventListener, View.OnTouchListener {

    int n = 2;
    Button btnarray[] = new Button[n];
    private SensorManager sManager;
    private TextView textView79;
    RelativeLayout RelativeLayout;
    int count = 0;
    double arg0, arg1, arg2;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.balance_test);

        RelativeLayout = (RelativeLayout)findViewById(R.id.RelativeLayout4);

        btnarray[0] = (Button)findViewById(R.id.button10);
        btnarray[1] = (Button)findViewById(R.id.button20);

        sManager = (SensorManager) getSystemService(SENSOR_SERVICE);

        textView79 = (TextView)findViewById(R.id.textView79);

        for(int i = 0; i <n; i++){
            btnarray[i].setOnClickListener(this);
        }

        RelativeLayout.setOnTouchListener(this);

    }

    //when this Activity starts
    @Override
    protected void onResume()
    {
        super.onResume();
        /*register the sensor listener to listen to the gyroscope sensor, use the
        callbacks defined in this class, and gather the sensor information as quick
        as possible*/
        sManager.registerListener(this, sManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_FASTEST);
    }

    @Override
    protected void onStop()
    {
        //unregister the sensor listener
        sManager.unregisterListener(this);
        super.onStop();
    }

    @Override
    public void onClick(View v){
        if(v == findViewById(R.id.button10)){
            Intent myIntent = new Intent(this, Results_Page_Activity.class);
            startActivity(myIntent);
        }

        if(v == findViewById(R.id.button20)){
            //implement
            int gyro_results = gyrotest();
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event){
        if(v == RelativeLayout && count ==0 ){
            gyrotest();
            count = 1;
            return true;
        }
        else{
            return false;
        }
    }

    public int gyrotest(){

        int gyro_results = 0;

        speakWords("Please place your hands on your hips and keep your feet under your shoulders. Hold your balance for the next 20 seconds");

        Thread.sleep(5000); //delay 5 seconds;
        int count = 0;
        int angmag = 0;
        while(count < 401){
            if (Math.sqrt((arg0*arg0 + arg1*arg1 + arg2*arg2)) > 20){
                 angmag = 1;
            }
            else{
                angmag = 0;
            }

            gyro_results = angmag + gyro_results;
            count++;
            Thread.sleep(50000);
        }

        return gyro_results;
    }

    @Override
    public void onAccuracyChanged(Sensor arg0, int arg1)
    {
        //Do nothing.
    }


    public void onSensorChanged(SensorEvent event)
    {
        //if sensor is unreliable, return void
        if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
        {
            return;
        }

        arg0 = event.values[0];
        arg1 = event.values[1];
        arg2 = event.values[2];

        //else it will output the Roll, Pitch and Yawn values
        textView79.setText("Orientation X (Roll) :"+ Float.toString(event.values[2]) +"\n"+
                "Orientation Y (Pitch) :"+ Float.toString(event.values[1]) +"\n"+
                "Orientation Z (Yaw) :"+ Float.toString(event.values[0]));
    }
}

I am assuming the code should look something like this:

delay 5 seconds;
int count = 0;
while count < 401{
    gyro_results = angmag + gyro_results;
    count++;
    delay 50 milliseconds;
}

return gyro_results;

I wish I were able to figure out how to code this on my own, but I've tried most methods. Thanks in advanced for any help :)

Edit: added in modified code with Thread.sleep function. However, I'm still seeing some issues with Thread.sleep.


Solution

  • Used sleep() method in Thread for delay process.

    try
    {
    Thread.sleep(5000);    //delay 5 seconds;
    int count = 0;
    while count < 401{
    gyro_results = angmag + gyro_results;
    count++;
    Thread.sleep(50000);    //delay 50 milliseconds
    }
    }catch(InterruptedException ie)
    {
    //Log message if required.
    }