OnSensorChanged () is not called

Today I have a lot of problems with sensors, but it seems that due to onSensorChanged () is not being called. Sorry for a possible duplicate question, but I have not seen any solutions. Here is 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 saw a lot of similar codes in different forums, but the appart from there and some other topics without solutions, I have not seen such a problem ... Do I need to add something to AndroidManifest? Is this a common problem? Is that the solution there?

Thank you Thomas (sorry for my bad English, I'm French ^^)

+4
source share
2 answers

, , SensorManager, , . , onSensorChanged?

Android , onResume():

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

, .

Edit:

, . :

  • , ... Toast.show() . Log . .
  • float int onSensorChanged(), 0 (1/-1, )

, :

@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);
}

+5

, Sensor.TYPE_ROTATION_VECTOR. rotation_vector null.

0

All Articles