Apps running in the background are not allowed to use sensor resources.
// code-linter.json5
{
"rules": {
"@performance/reasonable-sensor-use-check": "suggestion",
}
} import { UIAbility } from '@kit.AbilityKit';
import { sensor } from '@kit.SensorServiceKit';
export default class EntryAbility extends UIAbility {
onForeground(): void {
// In the foreground, listen to the required type of sensor based on the service requirements
sensor.on(sensor.SensorId.ACCELEROMETER, (data: sensor.AccelerometerResponse) => {
});
}
onBackground(): void {
// The background cancels the listening
sensor.off(sensor.SensorId.ACCELEROMETER);
}
} import { UIAbility } from '@kit.AbilityKit';
import { sensor } from '@kit.SensorServiceKit';
export default class EntryAbility extends UIAbility {
onForeground(): void {
// In the foreground, listen to the required type of sensor based on the service requirements
sensor.on(sensor.SensorId.ACCELEROMETER, (data: sensor.AccelerometerResponse) => {
});
}
onBackground(): void {
}
} plugin:@performance/all
For details about how to configure Code Linter check rules, see Code Linter.