简体   繁体   English

获取共享首选项特定值的最后更新日期和时间

[英]Get shared preferences specific value last updated date and time

I am using shared preferences to store userId once user login to the app. 用户登录应用后,我使用共享首选项存储userId。 I need to expire(delete) the stored userId after 30 days once the userId is stored on shared preferences. 一旦在共享首选项上存储了userId之后,我需要在30天后过期(删除)存储的userId。 Is it possible to get the last modified date and time of specific shared preference value. 是否可以获得特定共享首选项值的最后修改日期和时间。

It's not possible, SharedPreferences don't have any built in method to do this. 这是不可能的, SharedPreferences没有任何内置方法可以做到这一点。 You'll have to store last modification date manually, possible in SharedPreferences as well. 您必须手动存储上次修改日期,也可以在SharedPreferences存储。

I would say you can get the path of the SharedPreferences file and try using lastModified() of File class like, 我会说您可以获取SharedPreferences文件的路径,然后尝试使用File类的lastModified()

File file = new File("path of shared-preference file");
file.lastModified();

Actually you could create an Alarm : you can set it to run within 30 days from the "registration" for the value stored in SharedPreferences . 实际上,您可以创建一个Alarm :您可以将其设置为在“注册”后的30天内针对SharedPreferences存储的值运行。 You can then use a Receiver (search for more examples) to catch the alarm and run your code. 然后,您可以使用Receiver(搜索更多示例)来捕获警报并运行代码。 An example here 这里的一个例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM