Skip to content
Advertisement

How can i save the date and time of a task using shared preferences?

I am trying to store the time at which a BLE scanner successfully scans for broadcasted advertisement packets. So far i am using the

JavaScript

Function which successfully allows me to print the time of scans.

I am also using SharedPreferences() to store the information recieved from the scanned data such as RSSI and hex stream. However, i want to be able to be able to store the time of the scan also. I have made a code that compiles with no errors but when i try to run the app on my phone it crashes. How would i be able to store the Time using shared preferences?

(All i need is the time of scan not the date and time so if there is a better function than the calendar one that would be great!)

Code:

JavaScript

Advertisement

Answer

You are explicitly casting Date to Set<String> in the following line:

JavaScript

and it obviously will cause ClassCastException like class java.util.Date cannot be cast to class java.util.Set

You should use some different way to transform this Date object to String (or Set<String> if you need this for some reason) like

JavaScript

or similar.

IMHO you should think again about whole this mechanism and probably instead of String date keep in preferences some kind of ‘timestamp’ or another standard format for keeping the data and share it between systems


Read more here:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement