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.
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.
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.
Related
I am implementing the client-server communication for an app on android. I have some high-level questions before I start to code. Apologize if the question turns out vague or confusing. I am really new to this (2 days of Google-ing is all I have), and I haven't coded anything because this design issue I am asking.
Background: Our android app records users activities. These activities are, for example, the user bookmarks an article. Eventually we want to synchronize these activities with server's database. However this sync is not time-critical, meaning it is not necessary that we send the activity to the server every time there is a new activity.
Current Design: Every time there is an activity, we get the activities and put it in a Java object, mark as "notSync", then make an update call to the database (SQLite) on the mobile. Every 5 minutes or so we make a call into the database, pull the nonSync items into an array of objects, convert it to JSON, and send it to the server.
Question 1: It seems rather inefficient to put an object into database, then every a few minutes pull it out from database as an array of objects, then convert to JSON. Would that be a better design, if we just append all the activities to an array of objects, then convert to JSON directly from that array and clear that array? My dilemma is that, If we implement the latter, there might potentially be a huge array floating around in phone's memory. How do we choose what is the best to implement?
Question 2 When sync mobile SQLite with server MySQL, is our design (using java to pull the rows out as an array of objects, then converting it to JSON) a reasonable practice?
Thanks in advance!
The first thing to keep in mind is where the bottlenecks really are. In my experience communicating with a MySQL server, the connection takes by far the longest, followed by uploading/downloading data, followed by anything I did on the phone. So even without knowing exactly what you're going to be recording, I think it's a safe bet that using SQLite and creating objects will not have a big time impact compared to your network activity.
I would run some basic tests to measure how long various operations take, but to answer your questions:
If you clear the array properly, you won't have to worry about it growing too big and, if your array ever did get that big, you probably have other things to worry about (uploading many MBs of data). However, if your user does something for 4 minutes, then quits the app or something, they'll lose their data. Storing it in the DB is safer.
JSON seems to be the best way to send your data, it's quite efficient for that, and as I said above, I don't thing the SQLite/object creation time will be too great.
I have couple of applications that implements some login logic. If lets say one application is logged to some_account#gmail.com I want that all of these applications be logged to some_account#gmail.com. If I logout I want to all application do the same. But I don't want to immediately do the same. Application itself can handle it, but it need to know if some other application is already logged in and if yes just log in for the same email address as this app. So I need to know what is the email address for which other app is logged. I need to store one string.
First I was thinking about SharedPreferences, but this is rather bad idea because there are other options (and stackoverflow is full of bad example of SharedPreferences usage between processes). Despite it I tried this. Set up sharedUserId on all apps, called createPackageContext and eventually try to get preferences. But I cannot read from it. Always I got null, even if I used Context.Mode_WORLD_READABLE - which is deprecated by the way.
Ok, lesson learned do not use SharedPreferences for that (I suppose). But everything what I need now is to store single string somewhere where it could be read by other my apps.
Maybe I should use ContentProvider? But seriously... for one string?
What is the other option? I am sure that for so simple operation I really don't need Service or ContentProvider, but I actually haven't got idea how to do that.
You could use a broadcast receiver. All you would have to do is send a broadcast to application B when the data changes in application A. Then application B can handle the data in the background, and store it however you need to. It might be a bit of over kill, and there could be a better way to do it, but it would work.
I'm trying to make my own gps-tracker, mainly for bike-rides. I managed to make a usable app, at least for personal use, but I wanted to improve it, and have added ContentProviders, Fragments, CursorAdapters and a Service to receive the onLocationChanges from GPS.
However, I have really no idea how to handle the stream of Locations I'm receiving. My old app just saved them to an ArrayList, so right now my Service is sending a Broadcast to my Activity, that saves them to the ArrayList.
Problem is, that when the ride is over, it takes from 5-15 seconds to save the locations to sqlite (yes, I'm using compiledstatement and transaction), and I would like to avoid that, by saving the locations when received. When I tried to do that, my app became unresponsive (as expected), as I was doing the insert in the UI thread, and I do receive location updates often.
This is of course the most important aspect of the app, so what is the solution?
I could do the insert in a thread, but since inserting a single record is expensive, I'm not sure it could keep up.
I could write 25 records (or more) at a time in a transaction, but there will be some logic to keep track of what is saved and what is not.
Is there other options for a better user-experience ?
Use an IntentService to delegate the saving to another thread, then use applyBatch to do inserts.
Personaly I think the gps track would be better in a file than embeded in the database tracks are lickly to be just big. It's the same senario as pictures where embeding in the database is not the recomended solution. The databases would just hold some summary information and a reference to the file. You wouild write out the file as you go so no big pauses at the end. You keep the local ArrayList as well while you are recording if you are using it for displaing a path on a map or a graphical plot etc. That's the way I am doing things in my biking app IpBike.
I am developping an application that retrieves some data from a server.
I have two options:
Get the whole data set from the server and then work with it using the pattern 'singleton' to reduce the number of queries
For each query, get the corresponding data set from the server
What is the best choice?
In my opinion it depends.
It depends on the size of the data and if it even makes sense to return data that your user may not even need.
Personally, in my app that I am building I will be returning only the data that is required at that time. Obviously though, once I have the data I won't be fetching it again if it makes sense to keep hold of it for good or even just temporarily.
I agree with C0deAttack's answer. Your goal should be to minimize network traffic within the constraints of your app being a "good citizen" on the phone. (That means that your app does not negatively impact the user or other applications by using too many resources — including memory and file space.)
From the sound of it, I'm guessing that the data are not that voluminous. If so, I would recommend caching the response and use it locally, thus avoiding repeated queries to the server. Depending on how often the data changes, you might even consider making it persistent, so that the app doesn't have to query the server the next time it starts up. If the response includes an estimated time before it is considered outdated, that would help in establishing an update schedule. (Google's license server uses this idea.)
P.S. I don't see that this has anything (directly) to do with a singleton pattern.
How about storing your data in an sqlite database and do your queries with sql? Since you get sorting and ordering for free, it can help you writing less code. Also you have instant offline functionality if your user has no internet connection. :)
Ddms tells that, when I recall my class called in the past, it performs an onCreate() instead of onResume() that I expected...
I noticed that values that I stored in variables of my class in this case are lost and are null.
I presume that Android decide to do so to free memory resources (isn't it?).
I know that I could use Sharedpreferences to store data in a persistent way and then retrieve... But this is a really dirty way, in my opinion.
So, my question: how to have variables' values preserved also after an onDestroy() (I think?) that Android decided automatically?
Android will terminate your process at any time when you have no visible activities. For example, the user might go into Settings and terminate your app.
Static data members (my interpretation of your "variables of my class" description) are only meant to be caches, at best. They are no substitute for a persistent data model, whether you use a database, an XML file, a JSON file, or whatever.
So, if you want "variables' values preserved", save them someplace persistent.
You might find this page on data storage helpful. If your data is primitive, SharedPreferences are the recommended route. (Why do you think they are dirty?) If you need to store an object, you can use internal storage, as documented on that page.
http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)
If you don't like SharedPreferences, then you might want to look into Content Providers Even though Content Providers share data across applications, they also provide functionality for you to store persistent data in SQLlite and files that are available only to your app. In this case data stored in this fashion will be available even after closing and restarting your app.
You can save dynamic state by passing name:value pairs or serializable objects using the Android Architecture and the methods onSaveInstanceState and onRetainNonConfigurationState. You can persist state as explained in the other answers by writing to prefs and doing database writes.
I've been using custom Application class to store data over application life line.
How to declare global variables in Android?