2016-03-13 37 views
-1

我有一些代碼旨在計算用戶轉多快的次數。我實現了一個陀螺儀,並在Resume上有一些代碼工作,以收集數據以及一些語音輸出。但是,我有問題讓陀螺儀在onResume中正常工作。首先,語音輸出無法正常工作,並且出現故障。接下來,我仍然沒有獲得陀螺儀的價值。如何從onResume訪問陀螺儀?

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 <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 < 334){ 
        mSensorManager.registerListener(this, default_gyro, SensorManager.SENSOR_DELAY_UI); 
        if(omegaMagnitude > 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 < 334){ 
       mSensorManager.registerListener(this, default_gyro, SensorManager.SENSOR_DELAY_UI); 
       if(omegaMagnitude > 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])); 
    } 
} 

我該如何解決這段代碼,以便我可以從陀螺儀中實際檢索到數值?在此先感謝:)

+0

這是**你第三次問這個**,你不會得到一個有用的答案,直到你停止刪除你的問題,並面對他們暴露的基本事實。您**不能**在生命週期方法中獲取傳感器數據,並且您**不能在生命週期方法中有長時間運行的循環。正如你上次被問到的那樣,你必須得到傳感器回調中的數據,然後你可以將它郵寄到需要的地方。如果你想要一個長時間運行的循環,你將不得不使用後臺線程機制之一。 –

+0

首先,我改變了我的代碼,完全改變了問題,所以我沒有問過同樣的問題。在我提出的任何其他問題中,您都不會看到任何回答我的問題的評論。我問的只是一些事情要澄清,爲什麼我的代碼是壞的。您還沒有發佈解決方案來解決我的問題,也沒有指出我可以採用哪種解決方案。我刪除了一個在重大更改我的代碼之後變得多餘的問題,並且發現我犯了一個嚴重的錯誤。請提供一些形式的指導,說明我可以如何前進而不是攻擊。 – sjgandhi2312

+0

你的改變並沒有改變你的方法的**基本問題**,而只是以不相干的方式移動它。你被告知如何解決這個問題,但是你忽略了這一點,並刪除了你被告知的問題。現在你又被告知同樣的事情了 - 在你已經接受的答案中,儘管你仍然不願意接受它實際上告訴你如何徹底改變你的計劃的組織一個事件驅動的模型。 –

回答

0

好的,這裏有幾個問題。

1)永不睡在onResume或UI線程中的任何其他地方。你不能這樣舉起來。最好這樣做會導致你看起來像你已經墜毀,在最壞的情況下,它會實際上由於看門狗而崩潰。它幾乎不會做你想做的事。

2)你從來沒有真正設置imegaMagnitude,因爲你擁有了它重新定義onSensorChanged

3)你不會看到正確的價值無論如何,因爲它不會被置位,直到onSensorChanged被調用。因爲這只是在UI線程上調用的,所以直到onResume完成後纔會調用它(參見1)。所以你的代碼永遠無法工作。

4)你不能自己調用​​onResume。只有框架被允許調用生命週期功能。自己這樣做會嚴重地搞砸了。

你的方法是完全不合適的。重寫您的邏輯以使用基於事件的處理和狀態機模式。此代碼不可挽回。

+0

你會如何建議我製作新的代碼?我最初嘗試通過創建一個帶睡眠的新函數來編寫代碼,但我知道我無法從那裏訪問陀螺儀。 – sjgandhi2312