2012-01-13 8 views
8

मैं अपने एंड्रॉयड फोन से है, लेकिन कोई सफलता अब तक के साथ झुकाव कोण, मेरे सेंसरों से रोल कोण पाने के लिए कोशिश कर रहा हूँ मिलता है,झुकाव कोण एंड्रॉयड

जब मैं अपने बटन जो मुझे 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.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

धन्यवाद, कि यह किया –

+0

इन कोण बहुत छोटा मूल्य देता है और यहां तक ​​कि अगर सपाट सतह पर रखा बदलते –

+0

@AashishVirendraKBhatnagar मूल्यों रेडियन में कर रहे हैं रहता है + सेंसर इतना सटीक नहीं हैं –

संबंधित मुद्दे