D
D
Denis2017-10-22 18:39:06
Android
Denis, 2017-10-22 18:39:06

Why do sensors stop working in android?

Good day to all!
I ran into a problem, the phone's position sensors, after about 20 minutes of continuous operation, they start sending zeros.
What could be the reason for this and is there a way to check this moment, other than constantly looking for a series of identical answers?
Here is the code for working with sensors. The data is taken from them in the Service and written to a file.

protected void onResume() {
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        if(checkGRAVITY(sensorManager)) {
            sensorAccel = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
        }else {
            sensorAccel = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        }
        sensorMagnet = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
        sensorManager.registerListener(listener, sensorAccel, SensorManager.SENSOR_DELAY_NORMAL);
        sensorManager.registerListener(listener, sensorMagnet, SensorManager.SENSOR_DELAY_NORMAL);

        timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                getDeviceOrientation();
                //getActualDeviceOrientation();
                showInfo();
            }
        };
        timer.schedule(task, 0, 500);

        WindowManager windowManager = ((WindowManager) getSystemService(Context.WINDOW_SERVICE));
        Display display = windowManager.getDefaultDisplay();
        rotation = display.getRotation();
    }

SensorEventListener listener = new SensorEventListener() {

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

        @Override
        public void onSensorChanged(SensorEvent event) {
            switch (event.sensor.getType()) {
                case Sensor.TYPE_ACCELEROMETER:
                    for (int i=0; i < 3; i++){
                        valuesAccel[i] = event.values[i];
                    }
                    break;
                case Sensor.TYPE_GRAVITY:
                    for (int i=0; i < 3; i++){
                        valuesAccel[i] = event.values[i];
                    }
                    break;
                case Sensor.TYPE_MAGNETIC_FIELD:
                    for (int i=0; i < 3; i++){
                        valuesMagnet[i] = event.values[i];
                    }
                    break;
            }
        }
    };

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question