2012-01-13 114 views
8

我想從我的Android手機,但沒有成功,到目前爲止獲得的傾斜角度,從我的傳感器滾動角,得到傾斜角度的Android

當我點擊我的按鈕,應該給我的3個角度,我得到的「成果:0.0 -0.0 -0.0」

package com.example; 

import android.app.Activity; 
import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 
import android.os.Bundle; 
import android.widget.TextView; 
import android.widget.Button; 
import android.view.View; 


public class MyActivity extends Activity 
{ 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     final float[] mValuesMagnet  = new float[3]; 
     final float[] mValuesAccel  = new float[3]; 
     final float[] mValuesOrientation = new float[3]; 
     final float[] mRotationMatrix = new float[9]; 

     final Button btn_valider = (Button) findViewById(R.id.btn1); 
     final TextView txt1 = (TextView) findViewById(R.id.textView1); 
     final SensorEventListener mEventListener = new SensorEventListener() { 
      public void onAccuracyChanged(Sensor sensor, int accuracy) { 
      } 

      public void onSensorChanged(SensorEvent event) { 
       // Handle the events for which we registered 
       switch (event.sensor.getType()) { 
        case Sensor.TYPE_ACCELEROMETER: 
         System.arraycopy(event.values, 0, mValuesAccel, 0, 3); 
         break; 

        case Sensor.TYPE_MAGNETIC_FIELD: 
         System.arraycopy(event.values, 0, mValuesMagnet, 0, 3); 
         break; 
       } 
      }; 
     }; 
     btn_valider.setOnClickListener(new View.OnClickListener() 
     { 
      public void onClick(View view) 
      { 
       SensorManager.getRotationMatrix(mRotationMatrix, null, mValuesAccel, mValuesMagnet); 
       SensorManager.getOrientation(mRotationMatrix, mValuesOrientation); 
       final CharSequence test; 
       test = "results: " + mValuesOrientation[0] +" "+mValuesOrientation[1]+ " "+ mValuesOrientation[2]; 
       txt1.setText(test); 
      } 
     }); 

    } 
} 

回答

17

你幾乎沒有與代碼邁克,但使數據由事件偵聽器的任何傳感器的檢索,事件偵聽器和傳感器想要使用以下方法註冊SensorManager類:

SensorManager _sensorManager; 

_sensorManager.registerListener(mEventListener, _sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
       SensorManager.SENSOR_DELAY_NORMAL); 

下面是您的代碼,用於顯示我相信是您想要查看的數據。

import android.app.Activity; 
import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 
import android.os.Bundle; 
import android.widget.TextView; 
import android.widget.Button; 
import android.view.View; 


public class StackOverflowTestingGroundActivity extends Activity 
{ 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main);   

     SensorManager sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);   

     final float[] mValuesMagnet  = new float[3]; 
     final float[] mValuesAccel  = new float[3]; 
     final float[] mValuesOrientation = new float[3]; 
     final float[] mRotationMatrix = new float[9]; 

     final Button btn_valider = (Button) findViewById(R.id.btn1); 
     final TextView txt1 = (TextView) findViewById(R.id.textView1); 
     final SensorEventListener mEventListener = new SensorEventListener() { 
      public void onAccuracyChanged(Sensor sensor, int accuracy) { 
      } 

      public void onSensorChanged(SensorEvent event) { 
       // Handle the events for which we registered 
       switch (event.sensor.getType()) { 
        case Sensor.TYPE_ACCELEROMETER: 
         System.arraycopy(event.values, 0, mValuesAccel, 0, 3); 
         break; 

        case Sensor.TYPE_MAGNETIC_FIELD: 
         System.arraycopy(event.values, 0, mValuesMagnet, 0, 3); 
         break; 
       } 
      }; 
     }; 

     // You have set the event lisetner up, now just need to register this with the 
     // sensor manager along with the sensor wanted. 
     setListners(sensorManager, mEventListener); 

     btn_valider.setOnClickListener(new View.OnClickListener() 
     { 
      public void onClick(View view) 
      { 
       SensorManager.getRotationMatrix(mRotationMatrix, null, mValuesAccel, mValuesMagnet); 
       SensorManager.getOrientation(mRotationMatrix, mValuesOrientation); 
       final CharSequence test; 
       test = "results: " + mValuesOrientation[0] +" "+mValuesOrientation[1]+ " "+ mValuesOrientation[2]; 
       txt1.setText(test); 
      } 
     }); 

    } 

    // Register the event listener and sensor type. 
    public void setListners(SensorManager sensorManager, SensorEventListener mEventListener) 
    { 
     sensorManager.registerListener(mEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
       SensorManager.SENSOR_DELAY_NORMAL); 
     sensorManager.registerListener(mEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), 
       SensorManager.SENSOR_DELAY_NORMAL); 
    } 
} 
+0

感謝,即做到了這些 – 2012-01-17 14:03:14

+0

角度給出非常小的值,並且即使保持在平坦的表面上保持改變 – 2013-03-25 12:40:42

+0

@AashishVirendraKBhatnagar值是弧度+傳感器不是如此精確 – 2014-03-20 14:22:18