1

I am using android studio with Android 2.3 emulator. And i cant find why "onSensorChanged" does not called when i change parameters of accelerometer. I searched for the solution through stackoverflow but nothing worked for me.

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity implements SensorEventListener {

    private SensorManager sensorManager;
    private Sensor sensor;
    private TextView myTextView;

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

        myTextView = (TextView) findViewById(R.id.textView3);
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

        sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(this,
                sensor,
                SensorManager.SENSOR_DELAY_NORMAL);
        myTextView.setText("Waiting...");
    }

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

    }

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

    @Override
    public void onSensorChanged(SensorEvent event) {
        Sensor mySensor = event.sensor;

        if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            float x = event.values[0];
            float y = event.values[1];
            float z = event.values[2];

            myTextView.setText((int) x);
        }
    }

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

    }

}

As you see i am changing textView3 to the value "Waiting..." and after running the app it is still "Waiting...". Even after i change the parameters of Accelerometer sensor, but it must show the X value.

Emulator screenshot of the program

Sensors parameters

Update 1

Deleting the "sensorManager.registerListener(this,sensor, SensorManager.SENSOR_DELAY_NORMAL);" in "onCreate" does not solve the problem. And i need exactly Android 2.3

Update 2

Seems like API 9 does not support Accelerometer emulator. Source: https://stackoverflow.com/a/10612952/7878803

Kirill
  • 414
  • 7
  • 16
  • You are not using Android 3.2 but you have a 3.2inches devices with API9 (Android 2.3). Try using a more recent one. And call `registerListener` only in `onResume`. – rciovati Jul 21 '17 at 06:11
  • @rciovati sorry, missed the right version, edited the post. Deleting the "sensorManager.registerListener(this,sensor, SensorManager.SENSOR_DELAY_NORMAL);" in "onCreate" does not solve the problem. – Kirill Jul 21 '17 at 06:14

0 Answers0