0

Okay, so this is my problem: I am making an app that recieves sms messages, and can sort through them and such. it all works fine, but i am at the point where i have to make the class that will cause the phone to vibrate, play a sound and light the screen when the sms is recieved. you log into the system, and therefore different users will have different ways they want the app to notify when an sms is received. i am making a class that, when constructed, reads the users settings for the 3 notification methods as ints from a file, can save new values for these ints, and can then do the actual notifying. ive made it so that when the object is constructed it takes the username as a parameter, and then reads and writes the settings from a file specific to him. My question is this: what is the best way of saving and retrieving these ints from a file? I know about SharedPreferences, and it is very easy to use, but how much can i trust this? if i close the app, turn off the phone, and start everything back up again, is the information still there? I am currently thinking of making a small class with 3 public int fields, that implements java.io.Serializable, and then saves the whole object to a file, as then its easy to get the specific ints when i need them (keys/values, like SharedPreferences), but ive read that this is a quite slow process, and since the app is gonna recieve ALOT of messages(could be more than once a minute), having to read from a file at every recieve might make the app kinda heavy, since battery life is also important. So thats my question, should i make my own reading/writing file system, or can i trust SharedPreferences to never lose any data?

i went with sharedpreferences, and it works beautifully, thanks! =)

To elaborate on the whole performance thing of storing, how much data would need to be stored in order for an sqlite database to be worth it? because all these sms' have to be stored in a certain way, and since it will probably be hundreds at a time, is that enough for using an sqlite database? because i read somewhere that using a database is a bit slower than the io way, until you reach a certain amount of data? i am hoping the database is worth it, as its probably gonna make things alot easier down the road.

Gnurgen
  • 148
  • 3
  • 11

2 Answers2

1

SharedPreferences is safe for your saving the data. It's one of the recommended methods for storing data, see http://developer.android.com/guide/topics/data/data-storage.html

Another way, and also very safe, is to use a Sql table where you have the user name and those ints. Being just a few values this should be simple to implement. More information about this in the same link.

I do not see any reason to avoid using any of these 2 methods and go for the file system. They have been used many times before and they just work.

azertiti
  • 3,150
  • 17
  • 19
  • okay, so i assume that in "SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);" PREFS_NAME is the file where it is stored. so i could have one PREF_NAME that is just the general app where i put the name of the user, his login and such, and then i could have individual PREFS_NAME for each user where his individual settings are hidden. Pretty nifty, i just had to be sure i wouldnt lose any data this way, thanks =) – Gnurgen Apr 18 '12 at 20:10
  • You might gen into a too complicated design. Because you have one shared preference to keep login information for all users might be a problem to generate the proper keys. Use will probably end up having keys with a common prefix and a number at the end while reading them until you don't find one anymore. From the original question I didn't get the part with storing the username/login too. Taking all these into consideration I think using 2 SQL tables is a much better option. One for username + login and another for the preferences. – azertiti Apr 18 '12 at 20:20
  • the class i made can easily keep track of it, as in the general pref file there is always only one user, the one you are logged in as, and when the class is constructed, it takes that user, and makes/reads from a file with that name + a bit more to make sure they are different. that also ensures that even if someone were to log in with the exact name of the general pref file, it still wont be the same one, and they cant conflict =) but other than that, yes i would prefer to use databases for most data, but this is as simple as it gets, and works =) – Gnurgen Apr 18 '12 at 21:52
0

SharedPreferences should work perfectly for what you are doing. I implemented mine storing everything I need in the preference except password. Never once it's gone missing.

Also, SharedPreferences writes to a file as well SharedPreferences file

Unless you want these setting preserved even after the user delete the app, it suit your purpose.

If you insists to write a file, best way will probably be write it as xml or json. The latter would probably be faster by a fraction. Then load this file when appropriate.

Still, you should really consider hard to choose writing to a file over SharedPreferences. It's trust worthy! If you don't trust it, trust me and probably other people who writes Android and its applications.

Community
  • 1
  • 1
RobGThai
  • 5,937
  • 8
  • 41
  • 59
  • i just accomplished everything i wanted, i now have a sharedpreferences file that stores which user is logged in, and then i have the class that gives each user his own sharedpreferences file, where he can store the 3 ints in, it actually works pretty cool =) – Gnurgen Apr 18 '12 at 21:27