4

I am coding an Android application but I have some trouble using the accelerometer. Strangely, my function onSensorChanged() is not called and I don't know why :/ Could you help me please ? :) Here's my code :

@SuppressLint("NewApi") 
public class Game extends Activity implements SensorEventListener {

SensorManager sensorManager;
ImageView vaisseau;
TextView test;
RelativeLayout rl;

@SuppressLint("NewApi") @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_GAME);

    setContentView(R.layout.game);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

    Display ecran = getWindowManager().getDefaultDisplay(); 
    int largeur= ecran.getWidth();
    int hauteur= ecran.getHeight();

    Drawable d = getResources().getDrawable(R.drawable.spaceship);
    int largeurVaisseau = d.getIntrinsicWidth();

    vaisseau = new ImageView(this);
    vaisseau.setImageResource(R.drawable.spaceship);
    rl = (RelativeLayout)findViewById(R.id.layout_game);
    vaisseau.setX(largeur/2 - largeurVaisseau);
    vaisseau.setY(hauteur-hauteur*10/100);
    rl.addView(vaisseau);

    test = new TextView(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return true;
}

public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
        test.setText(Float.toString(event.values[0]));
        rl.addView(test);
    }
}   

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {    
}

@Override
protected void onResume() {
    super.onResume();
    sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_GAME);
}

@Override
protected void onPause() {
    super.onPause();
    sensorManager.unregisterListener(this);
}
}

Thanks in advance :), Jeffrey

user2363392
  • 95
  • 1
  • 7

1 Answers1

1

Register for the sensor Sensor.TYPE_ACCELEROMETER rather than Sensor.TYPE_ORIENTATION.

Larry Schiefer
  • 15,687
  • 2
  • 27
  • 33
  • Thank you for your answer :) But it's still not working :/ Any ideas ? – user2363392 Aug 04 '14 at 21:07
  • You are registering 2x for the sensor (in `onCreate()` and in `onResume()`, which could cause problems. I doubt it would stop working completely (or never work), but it could be problematic. You probably just want to go with the one in `onResume()`. Have you tried attaching the debugger or logging sensor changes rather than directly updating the UI? Perhaps the UI isn't working but the sensor changes are happening? – Larry Schiefer Aug 05 '14 at 10:54
  • I recommend this for decent reference code: https://stackoverflow.com/a/10291428/550471 – Someone Somewhere Sep 06 '17 at 10:54