How can I access the gyroscope from onResume?
<p>I have some code that aims to count how many times the user turns too quickly. I implemented a gyroscope and have some code working within on Resume to collect the data, along with some voice outputs. However, I am having problems making the gyroscope work properly within onResume. For one, the voice outputs do not work properly, and goes out of order. Next, I still am not getting values for the gyroscope. </p>

<pre><code>package com.example.shivamgandhi.gyrosafe;

import android.content.Context;
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.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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 mSensorManager;
    private Sensor default_gyro;
    private TextView textView79;
    RelativeLayout RelativeLayout;
    int count1 = 0;
    float axisX, axisY, axisZ;
    double omegaMagnitude;
    EditText ed29;
    private static final float NS2S = 1.0f / 1000000000.0f;
    private final float[] deltaRotationVector = new float[4];
    int touch = 0;

    int count2 = 0;
    int time1 = 0;
    int time2 = 0;

    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);

        mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        default_gyro = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);

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

        ed29 = (EditText)findViewById(R.id.editText29);

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

        RelativeLayout.setOnTouchListener(this);

    }

    //when this Activity starts
    @Override
    protected void onResume()
    {
        super.onResume();

        if (touch == 1){
            try{
                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(2000);
                while(time1 &lt; 334){
                    mSensorManager.registerListener(this, default_gyro, SensorManager.SENSOR_DELAY_UI);
                    if(omegaMagnitude &gt; 2){
                        count1 ++;
                        time1 ++;
                    }
                    else{
                        time1 ++;
                    }
                }
                Thread.sleep(2000);
                speakWords("Place your hands by your side. Tap to continue.");
                Thread.sleep(1000);
                touch = 10;
            }
            catch(InterruptedException e){
                e.printStackTrace();
            }
        }

        if (touch == 2){
            speakWords("Please place your hands on your hips and stand on your nondominant foot for the next 20 seconds");
            while(time2 &lt; 334){
                mSensorManager.registerListener(this, default_gyro, SensorManager.SENSOR_DELAY_UI);
                if(omegaMagnitude &gt; 2){
                    count2 ++;
                    time1 ++;
                }
                else{
                    time1 ++;
                }
            }
            speakWords("You may resume normal stance now");
            touch = 20;
        }


    }

    @Override
    protected void onStop()
    {
        //unregister the sensor listener
        mSensorManager.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);
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event){
        if(v == RelativeLayout){
           if(touch == 0){
               touch = 1;
               onResume();
               return true;
           }
            else if (touch == 10){
               touch = 2;
               onResume();
               return true;
           }
            return true;
        }
        else{
            return false;
        }
    }

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


    public void onSensorChanged(SensorEvent event) {
        // This timestep's delta rotation to be multiplied by the current rotation
        // after computing it from the gyro sample data.

        // Axis of the rotation sample, not normalized yet.
        float axisX = event.values[0];
        float axisY = event.values[1];
        float axisZ = event.values[2];

        // Calculate the angular speed of the sample
        double omegaMagnitude = Math.sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);

        float[] deltaRotationMatrix = new float[9];
        SensorManager.getRotationMatrixFromVector(deltaRotationMatrix, deltaRotationVector);

        textView79.setText("Gyro X (Roll) :" + Float.toString(event.values[2]) + "\n" +
                "Gyro Y (Pitch) :" + Float.toString(event.values[1]) + "\n" +
                "Gyro Z (Yaw) :" + Float.toString(event.values[0]));
    }
}
</code></pre>

<p>How do I go about fixing this code so that I can actually retrieve values from the gyroscope? Thanks in advance :)</p>