嘿你们所有人。我目前正在开发一款应用,我感兴趣的是如何在 24小时之后仅重置一个文件来自共享偏好设置。希望有人可以提供帮助:D
答案 0 :(得分:0)
context.getSharedPreferences("pref_file", 0).edit().clear().commit();
24小时后。
类似的东西:
//...
private ScheduledFuture mHandle;
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
//...
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
final Runnable deleteIt = new Runnable() {
public void run() {
getSharedPreferences("pref_file", 0).edit().clear().commit();
mHandle.cancel(false); //don't cancel here if you want it to run every 24 hours
}
};
if(mHandle == null)
mHandle = scheduler.scheduleAtFixedRate(deleteIt, 60 * 60 * 24, 60 * 60 * 24, SECONDS);
return START_STICKY;
}
//...
您还可以使用Timer。