我正在使用 React Native 和
@adrianso/react-native-device-brightness
库来实现此目的
想象一下我在屏幕 A 中,设备亮度约为 0.5
当重定向到屏幕 B 时,保存当前亮度,然后我将亮度设置为 1(最大值)。如果我返回屏幕 A,亮度将设置回保存的值 (0.5)
这将正常工作,直到:
会发生以下情况之一:
我已经尝试过:
expo-brightness
请问这个问题与iOS的线程阻塞有关吗?
const [previousBrightness, setPreviousBrightness] = useState<number | null>(null);
const getBrightness = () => {
Brightness.getBrightnessAsync().then((value) => {
setPreviousBrightness(value);
});
};
const restoreBrightness = useCallback(() => {
if (previousBrightness !== null) {
Brightness.setBrightnessAsync(previousBrightness)
.then(() => {
console.info('Brightness restored');
})
.catch(() => {
console.error('Error restoring brightness');
});
}
}, [previousBrightness]);
useEffect(() => {
(async () => {
getBrightness();
const { status } = await Brightness.requestPermissionsAsync();
if (status === 'granted') {
Brightness.setBrightnessAsync(1)
.then(() => {
console.info('Brightness set to 1');
})
.catch(() => {
console.error('Error setting brightness');
});
}
})();
return restoreBrightness;
},[restoreBrightness]);