1. Download our Official Android App: Forums for Android!

How can I get the values from the "public void onSensorChanged(SensorEvent event) " to another?

Discussion in 'Android Apps & Games' started by lugopaco, Sep 8, 2011.

  1. lugopaco

    lugopaco Member
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    30
    Joined:
    Mar 31, 2011

    Mar 31, 2011
    30
    0
    15
    Student
    Mexico
    Hi!! I'm making an application on Android and I'm using the accelerometer sensor of my phone.

    Part of the code is:
    Code (Text):
    1.  
    2.  
    3.     public class OtroacelbtActivity extends Activity implements SensorEventListener {
    4.     private float x = 0, y = 0, z = 0;
    5.    
    6.      @Override
    7.         public void onCreate(Bundle savedInstanceState) {
    8.             super.onCreate(savedInstanceState);
    9.     }
    10.    
    11.     @Override
    12.         protected void onResume() {
    13.             super.onResume();
    14.             SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
    15.             List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_ACCELEROMETER);
    16.             if (sensors.size() > 0) {
    17.              sm.registerListener(this, sensors.get(0), SensorManager.SENSOR_DELAY_UI );
    18.             }
    19.     }
    20.    
    21.     @Override
    22.     public void onAccuracyChanged(Sensor sensor, int accuracy) {}
    23.    
    24.     @Override
    25.     public void onSensorChanged(SensorEvent event) {
    26.             synchronized (this) {
    27.                
    28.             String message=null;
    29.            
    30.                
    31.                 x = event.values[0];
    32.                 y = event.values[1];
    33.                 z = event.values[2];
    34.     }
    35.     }
    36. }
    I want to use the x,y and z values on another method, but even they are global variables, if I call them just like that on the other method, thee only give the "null" value (cause it's a void method).

    How can I do it???

    Thanks.
     

    Advertisement

  2. barryirvine

    barryirvine Lurker
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Nov 1, 2011

    Nov 1, 2011
    1
    0
    5
    If you look at the AcceleratorPlay in the api-10 samples as part of the SDK you can see how they did it.

    Simply define some class level variables e.g.
    Code (Text):
    1. private float mSensorX;
    2. private float mSensorY;
    And in the onSensorChanged method

    Code (Text):
    1. switch (mDisplay.getOrientation()) {
    2. case Surface.ROTATION_0:
    3.     mSensorX = event.values[0];
    4.     mSensorY = event.values[1];
    5.     break;
    6. case Surface.ROTATION_90:
    7.     mSensorX = -event.values[1];
    8.     mSensorY = event.values[0];
    9.     break;
    10. case Surface.ROTATION_180:
    11.     mSensorX = -event.values[0];
    12.     mSensorY = -event.values[1];
    13.     break;
    14. case Surface.ROTATION_270:
    15.      mSensorX = event.values[1];
    16.      mSensorY = -event.values[0];
    17.      break;
    18. }
     

Share This Page

Loading...