Skip to content Skip to sidebar Skip to footer

Accelerometer Not Changing Values

So I have the code below which I can't get to work. I am trying to have one class (AccelerometerReader) read the values of my phone's accelerometer, and then I call those values in

Solution 1:

MyService.java

publicclassMyServiceextendsServiceimplementsSensorEventListener {
       publicvoidonCreate() {
              super.onCreate();
              sm = (SensorManager) getSystemService(SENSOR_SERVICE);
              sm.unregisterListener(this);  
              sm.registerListener(this,sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_FASTEST);
       }
       publicvoidonDestroy() {
              super.onDestroy();
              sm.unregisterListener(this);
       }

// Creates a new binder@Overridepublic IBinder onBind(Intent intent) {
       returnnewMyBinder<MyService>(this);
}

publicvoidonSensorChanged(SensorEvent event) {
       ax=event.values[0];                           
       ay=event.values[1];                           
       az=event.values[2];                
       for (Listener listener : listeners) listener.handleNewAccelValue(this);
}

publicinterfaceListener {
       // Actions to take when a new position has been added to the modelvoidhandleNewAccelValue(MyService sender);
}

// List of all the ongoing listeners bound to the ModelprivateList<Listener> listeners = newArrayList<Listener>();

// Binds a listener from the View to the ModelpublicvoidaddListener(Listener listener) {
       this.listeners.add(listener);
}

// Removes a listener from the View to the ModelpublicvoidremoveListener(Listener listener) {
       this.listeners.remove(listener);
}

YourActivity.java

publicclassYourActivityimplementsMyService.Listener{
publicvoidonCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState); 
        // Binds to the created servicedoBindService();
}

// Creating the controller and the modelprivateServiceComputeFinal controller;

// Creates the connection with a MyService serviceServiceConnection connection = newServiceConnection(){
    publicvoidonServiceConnected(ComponentName name, IBinder binder) {
        controller = ((MyBinder<MyService>) binder).getService();           
    }

    publicvoidonServiceDisconnected(ComponentName name) {
        controller = null;
    }   
};

// Binds to the created servicevoiddoBindService(){
    bindService(newIntent(this, MyService.class), connection, Context.BIND_AUTO_CREATE);
}

// And finally recover the accelerometer data that interest youpublicvoidhandleNewAccelValue(MyService sender){
    DO_WHATEVER_YOU_WANT_WITH(controller.getAx());
}

I gave you all the tools to do what you want. Try to look at this code and understand what it does (it's not really complicated), a ServiceConnection allows the Activity to recover values from the Service and the Listener interface allows the Service to notify the Activity when new values are ready. Feel free to ask anything if needed.

Solution 2:

The method you use looks really strange to me. You actually pull accelerometer values in your drawing class. It's like, you create an instance of the accelerometer class, then take away his values right away (without waiting for the real accelerometer to actually retrieve values) then try to draw it. That is probably your problem anyway, you draw the starting accelerometer values and don't refresh them when they change. I don't know what's the rest of your code, but if you onDraw is called several time, you just create a new instance of your class and again the values you pull out from it didn't have time to be changed by the sensor readings.

You would better use a push method, implementing a listener interface in the accelerometer class so whenever a new value of acceleration is retrieved, the drawing class is notified and gets the new values, then draw them.

edit : I highly recommend you to use a faster delay for your accelerometer (Fastest will do it). Keep in mind the accelero is the less power-consuming of them all.

Post a Comment for "Accelerometer Not Changing Values"