The android.hardware.SensorManager#getDefaultSensor offers two types of Motion Sensors:
TYPE_ROTATION_VECTOR: a combination of the gyroscope, accelerometer, and magnetometer. TYPE_GEOMAGNETIC_ROTATION_VECTOR: a combination of the accelerometer and magnetometer. 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.
Replace TYPE_ROTATION_VECTOR with TYPE_GEOMAGNETIC_ROTATION_VECTOR when retrieving the Motion Sensor.
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
// ..
}
//..
}
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
// ..
}
//..
}