0

I'm writing an app that show accelerometer readings, I was able yesterday to write all the accelerometer code in the main activity and it works fine on emulator and my device.

But today I was trying to make a class that contains all the accelerometer code and return the three integers X,Y,Z in integer array only, so I make a code that I think it`s right but every time I run the project on the emulator and it always give me 0 ,0,0 . So I wish any help , please .

activity code ::

package com.example.accelerometer_sensor;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

    int [] AccVal = new int[3];

    TextView acceleration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AccelerometerClass acc = new AccelerometerClass();
        acc.AccelerometerInit(this);
         AccVal =acc.Vals;

         acceleration = (TextView)findViewById(R.id.acceleration);


         acceleration.setText("X: "+acc.Vals[0]+
                    "\nY: "+acc.Vals[1]+
                    "\nZ: "+acc.Vals[2]);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


}

accelerometerClass :

package com.example.accelerometer_sensor;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class AccelerometerClass implements SensorEventListener 
{
    int [] Vals = new int[3];
    Sensor accelerometer;
    SensorManager sm;

    public void AccelerometerInit(Context context)
    {

        sm = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);       
        accelerometer=sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sm.registerListener(this, accelerometer,SensorManager.SENSOR_DELAY_NORMAL); 
    }

    protected void onResume() {

        sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);

    }

    protected void onPause() {

        sm.unregisterListener(this);
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub

        Vals[0]=(int)event.values[0];
        Vals[1]=(int)event.values[1];
        Vals[2]=(int)event.values[2];
    }


}

please i stuck in this problem for along time , any help will be appreciate .

MoRashad
  • 39
  • 1
  • 7
  • 2
    I think stackoverflow is the better place for this question. – Evorlor Apr 27 '15 at 22:57
  • How is your emulator configured? Maybe you should test your code on a real phone or have a look at this question: [How can I simulate accelerometer in android emulator?](http://stackoverflow.com/questions/3921467/how-can-i-simulate-accelerometer-in-android-emulator) – martijnn2008 Apr 28 '15 at 10:10

2 Answers2

0

I suspect that the text is being written before the onSensorChanged event is being fired. So it goes like this...

  1. Register listener
  2. Write text (all 0s at this point)
  3. Event fires and changes the x,y,z values

The text is already written though so the values aren't updating. I could be wrong but you could test this by stopping the thread for a few seconds before writing the values to the text field with a Thread.sleep(2000); 2000 is in milliseconds.

It could also be that the event isn't firing. You can check for this by adding a line inside the onSensorChanged() method that logs to your logcat.

Log.d(TAG, "onSensorChanged method has been invoked");

Edit: I just ran it. Everything works fine, but you're not getting the results you want because the text is being written before the event is firing. You need to update your UI when the onSensorChanged() method is called.

david2278
  • 414
  • 1
  • 4
  • 15
0

Implement getter method for object Vals in AccelerometerClass

public class AccelerometerClass implements SensorEventListener 
{
    int [] Vals = new int[3];
    Sensor accelerometer;
    SensorManager sm;
public AccelerometerClass(Context context)
{

    sm = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);       
    accelerometer=sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    sm.registerListener(this, accelerometer,SensorManager.SENSOR_DELAY_NORMAL); 
}

...

// getter method
public int getValue(int index) {
    return this.Vals[index];
}

}

Access it from MainActivity :

public class MainActivity extends Activity {

    int [] AccVal = new int[3];

    TextView acceleration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // this one will call constructor of AccelerometerClass and initialise sensors
        AccelerometerClass acc = new AccelerometerClass(getApplicationContext());



         acceleration = (TextView)findViewById(R.id.acceleration);

         // access like this here
         acceleration.setText("X: "+acc.getValue(0)+
                    "\nY: "+acc.getValue(1)+
                    "\nZ: "+acc.getValue(2));
    }
}

This will solve your problem

Kushal
  • 8,100
  • 9
  • 63
  • 82
  • i did as you mention above but it still 3 zeros on the emulator ??! any helppp please – MoRashad Apr 29 '15 at 12:59
  • Are you getting `onSensorChanged` event? Put logs inside this event to check – Kushal Apr 29 '15 at 13:12
  • sir , i have put this line in onsensorChanged() method : Log.d(TAG, "onSensorChanged method has been invoked"); and when i`m trying to run normally the app on the emulator it gives me loading sign(the circle load) and nothing appears . ?! what does this means ? – MoRashad Apr 29 '15 at 13:23
  • Then it means that Accelerometer is not giving sensor data change to our application.. In simpler terms, the accelerometer data is not changed.. Please check one time, by putting logs in `AccelerometerInit()` method and check if it is called.. It should be called.. If it is called already, then our code is right, but sensor is not chaging data so we are not getting – Kushal Apr 29 '15 at 13:37
  • sorry for disturbing you , but i did as you said put this line in AccelerometerInit() the first line :: Log.e(TAG, "onSensorChanged method has been invoked"); and i check logcat found this :: 04-29 15:50:16.866: E/(770): onSensorChanged method has been invoked , so as i understand from your previous comment ,so this method not calling right , please help me to calling this right . Thanks for your patience and your understanding . – MoRashad Apr 29 '15 at 15:52