Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onSensorChanged() is not called

I'm having a lot of trouble tonight with sensors, but it appear it is because of onSensorChanged() is not called. Sorry for the eventual duplicate question, but I didn't see any solutions. Here's my code :

public SensorManager manager;
public Sensor rotation_vector;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    manager = (SensorManager)getSystemService(SENSOR_SERVICE);
    rotation_vector = manager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
}

@Override
public void onSensorChanged(SensorEvent event) {
    int xValue = (int) event.values[0];
    int yValue = (int) event.values[1];
    int zValue = (int) event.values[2];
   Toast.makeText(getApplicationContext(),"this doesn't appear...",Toast.LENGTH_LONG);


@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    //TODO: we'll see later about it
}
public void onPause() {
        /*Et on le dés-enregistre quand on sort de l'activité, pour économiser de la batterie*/
        super.onPause();
        manager.unregisterListener(this);
}
@Override
    protected void onResume() {
        /*On enregistre le sensor quand l'utilisateur revient sur l'activité*/
        super.onResume();
        manager.registerListener(this, rotation_vector, SensorManager.SENSOR_DELAY_NORMAL);
        if (null != rotation_vector) {
        } else {
            Toast.makeText(getApplicationContext(),
                    "There is no gyroscope on your device",
                Toast.LENGTH_LONG).show();
        }
    }

I have seen a lot of similar code on different forums, but appart from there and some others topic without solutions, I haven't seen such a problem yet... Do I have to add something in AndroidManifest ? Is it a common problem ? It there a solution ?

Thanks, Thomas (sorry for my bad english, I'm French^^)

like image 514
Thomas Avatar asked Jan 26 '15 22:01

Thomas


1 Answers

I was not working with Sensors so far, but it looks like you have obtained SensorManager but did not register any listener for changes that should occur. Just ask yourself how could be onSensorChanged called?

Have a look at the sample on official android tutorial site about sensors and you could see this in onResume() method:

@Override
protected void onResume() {
    super.onResume();
    mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);
}

Also don't forget to study whole page it can help you a lot.

Edit:

I had to copy paste your sources because I haven't seen any problem. There are 2 basicaly:

  • Toast is not appearing because you haven't told him to... Toast.show() will display it. But you rather use Log to output changes into console. It's up to you
  • Do not retype float into int in onSensorChanged() method because all values are 0 or (1/-1 if you are lucky)

So result is following:

@Override
public void onSensorChanged(SensorEvent event) {
    float xValue = event.values[0];
    float yValue = event.values[1];
    float zValue = event.values[2];
    Log.d(LOG_TAG, "x:"+xValue +";y:"+yValue+";z:"+zValue);
}

other methods are correct

like image 157
VizGhar Avatar answered Oct 29 '22 20:10

VizGhar