Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Discussion > Android Applications

Android Applications All the information you could ever want about Android Applications. Learn about apps and get help with them... all here! New apps can be found and announced in the Applications Announcements forum linked below.



Reply
 
LinkBack Thread Tools
Old September 8th, 2011, 11:02 AM   #1 (permalink)
Junior Member
 
Join Date: Mar 2011
Location: Mexico
Posts: 30
 
Device(s): Samsung Galaxy 5 (GT-I5500L), Samsung Galaxy Ace
Thanks: 11
Thanked 2 Times in 2 Posts
Sad How can I get the values from the "public void onSensorChanged(SensorEvent event) " to another?

Hi!! I'm making an application on Android and I'm using the accelerometer sensor of my phone.

Part of the code is:
Code:
    public class OtroacelbtActivity extends Activity implements SensorEventListener {
    private float x = 0, y = 0, z = 0;
    
     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    }
    
    @Override
        protected void onResume() {
            super.onResume();
            SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
            List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_ACCELEROMETER);
            if (sensors.size() > 0) {
             sm.registerListener(this, sensors.get(0), SensorManager.SENSOR_DELAY_UI );
            }
    }
    
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {}
    
    @Override
    public void onSensorChanged(SensorEvent event) {
            synchronized (this) {
                
            String message=null;
           
                
                x = event.values[0];
                y = event.values[1];
                z = event.values[2];
    }
    }
}
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.

lugopaco is offline  
Reply With Quote
Sponsors
Old November 2nd, 2011, 03:43 PM   #2 (permalink)
New Member
 
Join Date: Nov 2011
Posts: 1
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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:
private float mSensorX;
private float mSensorY;
And in the onSensorChanged method

Code:
switch (mDisplay.getOrientation()) {
case Surface.ROTATION_0:
    mSensorX = event.values[0];
    mSensorY = event.values[1];
    break;
case Surface.ROTATION_90:
    mSensorX = -event.values[1];
    mSensorY = event.values[0];
    break;
case Surface.ROTATION_180:
    mSensorX = -event.values[0];
    mSensorY = -event.values[1];
    break;
case Surface.ROTATION_270:
     mSensorX = event.values[1];
     mSensorY = -event.values[0];
     break;
}
barryirvine is offline  
Last edited by barryirvine; November 2nd, 2011 at 03:47 PM.
Reply With Quote
Reply

Bookmarks

Tags
accelerometer, android, java, void


Go Back   Android Forums > Android Discussion > Android Applications User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 08:48 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo