The android.hardware.SensorManager#getDefaultSensor offers two types of Motion Sensors:

Why is this an issue?

The battery life is a major concern for mobile devices and choosing the right Sensor is very important to reduce the power usage and extend the battery life.

It is recommended, for reducing the power usage, to use TYPE_GEOMAGNETIC_ROTATION_VECTOR for background tasks, long-running tasks and other tasks not requiring accurate motion detection.

The rule reports an issue when android.hardware.SensorManager#getDefaultSensor uses TYPE_ROTATION_VECTOR instead of TYPE_GEOMAGNETIC_ROTATION_VECTOR.

How to fix it

Replace TYPE_ROTATION_VECTOR with TYPE_GEOMAGNETIC_ROTATION_VECTOR when retrieving the Motion Sensor.

Code examples

Noncompliant code example

public class BackGroundActivity extends Activity {

    private Sensor motionSensor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        motionSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR); // Noncompliant
        // ..
    }
    //..
}

Compliant solution

public class BackGroundActivity extends Activity {

    private Sensor motionSensor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        motionSensor = sensorManager.getDefaultSensor(Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR); // Compliant
        // ..
    }
    //..
}

Resources

Documentation