AsyncStorage getItem 和 setItem 在 Android 设备中不起作用
AsyncStorage getItem and setItem doesn't work in Android device
我正在尝试将当前日期的值(以毫秒为单位)存储在 AsyncStorage setItem 中,并在应用程序状态发生变化时使用 getItem 检索它。我正在使用 AppState 侦听器来更改状态和比较值。当我 运行 它在 ios 模拟器上时它工作正常但当我 运行 在 android 设备上时它不起作用。谁能帮帮我?
StoreDate = async () => {
console.log("Date",new Date().getTime().toString());
await AsyncStorage.setItem('date', new Date().getTime().toString());
};
retrieveDate = async () =>
{
try{
let value = await AsyncStorage.getItem('date')
var Intvalue = parseInt(value,10); // value of newDate().getTime() as Integer
console.log("Intvalue",Intvalue);
console.log("value",value);
Alert.alert(value);
return value;
}
catch(error){
Alert.alert(error);
}
};
_handleAppStateChange = (nextAppState) =>
if (this.state.appState.match(/inactive/) && nextAppState === 'background') {
let getDateTime = this.StoreDate().then((filter) => {
console.log("filter",filter);
console.log("getDateTime",getDateTime);
});
}
else if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') {
let currentDate = new Date().getTime();
let currentDateTimeStamp = Math.floor(currentDate / 1000);
console.log("currentDate",currentDate);
console.log("currentDateTimeStamp",currentDateTimeStamp);
let storedTime = this.retrieveDate().then((filter)=> {
console.log("filter",filter);
console.log("storedTime",storedTime);
var Intfilter = parseInt(filter,10);
console.log("Intfilter",Intfilter);
var IntfilterSeconds = Math.floor(Intfilter / 1000);
console.log("IntfilterSeconds",IntfilterSeconds);
var timeDiff = currentDateTimeStamp - IntfilterSeconds;
console.log("timeDiff",timeDiff)
if(timeDiff > 300){
** DO Something **
})
}
else{
** Do Something **
})
}
});
}
此问题有解决方法。
我发现这不是 AsyncStorage 的问题,而是 AppState 的问题。
android中主要有两个appstate:Active和Background。
当应用程序处于后台时处理 appState match(active|background) && nextAppState='background' 确实为我解决了问题。
我正在尝试将当前日期的值(以毫秒为单位)存储在 AsyncStorage setItem 中,并在应用程序状态发生变化时使用 getItem 检索它。我正在使用 AppState 侦听器来更改状态和比较值。当我 运行 它在 ios 模拟器上时它工作正常但当我 运行 在 android 设备上时它不起作用。谁能帮帮我?
StoreDate = async () => {
console.log("Date",new Date().getTime().toString());
await AsyncStorage.setItem('date', new Date().getTime().toString());
};
retrieveDate = async () =>
{
try{
let value = await AsyncStorage.getItem('date')
var Intvalue = parseInt(value,10); // value of newDate().getTime() as Integer
console.log("Intvalue",Intvalue);
console.log("value",value);
Alert.alert(value);
return value;
}
catch(error){
Alert.alert(error);
}
};
_handleAppStateChange = (nextAppState) =>
if (this.state.appState.match(/inactive/) && nextAppState === 'background') {
let getDateTime = this.StoreDate().then((filter) => {
console.log("filter",filter);
console.log("getDateTime",getDateTime);
});
}
else if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') {
let currentDate = new Date().getTime();
let currentDateTimeStamp = Math.floor(currentDate / 1000);
console.log("currentDate",currentDate);
console.log("currentDateTimeStamp",currentDateTimeStamp);
let storedTime = this.retrieveDate().then((filter)=> {
console.log("filter",filter);
console.log("storedTime",storedTime);
var Intfilter = parseInt(filter,10);
console.log("Intfilter",Intfilter);
var IntfilterSeconds = Math.floor(Intfilter / 1000);
console.log("IntfilterSeconds",IntfilterSeconds);
var timeDiff = currentDateTimeStamp - IntfilterSeconds;
console.log("timeDiff",timeDiff)
if(timeDiff > 300){
** DO Something **
})
}
else{
** Do Something **
})
}
});
}
此问题有解决方法。 我发现这不是 AsyncStorage 的问题,而是 AppState 的问题。 android中主要有两个appstate:Active和Background。 当应用程序处于后台时处理 appState match(active|background) && nextAppState='background' 确实为我解决了问题。