Sharing small amount of data between apps - android

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.

Related

best method of writing/reading simple values in a file in android?

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.

How to get a value from another android application?

I'm building 2 applications.
The first application, on start, asks a value (for example an integer) to the second application, which may or may not be installed and may or may not be running.
I don't think shared files or preferences are the best option, because I want to provide different output when the second application is present or not.
Should I use Intents? Content provider? Anything else?
Use Intents and Activities, if you want to retrieve a user entered value, or something that requires user interaction.
ContentProvider would also work, depending on your requirements.
I solved by creating an Intent with putExtra and finish() (to return to first app). 4 lines of code on each app.

ANDROID - class Application

I'm trying to understand how the Application class.
I've noticed that need to declare it in <application> manifest within the tag and then can access the variables in other classes as they were global variables. And even out of the application the value of these varieties do not change.
However, if you unplug the phone, the next time you turn it on and start applying the value of the variables returned to its initial state.
I wonder if you can maintain the state of variables when we turned off the phone and reconnect it?
Application data is available as long as your application is "active". When the OS decides to terminate it to clear memory, so goes your application data (you typically don't control when this happens, as per the mobile development good practices: the OS decides on its own), and it's not persisted for the next time you start the app. So anything you store in the Application should be stored again each time the app is started.
It should be used to keep short-term data available to you. A good use case is when you need to access a complex data structure from multiple activities: it's not possible to use bundles for that. You can generate your complex data structure in your start activity, store it in the application, and then retrieve it in any other application that may need it.
But you should not use it for long-term persistent data. For that, the best is to use a SQLite database.
I'm not sure I fully understand what you mean, but it seems like you want to use Shared Preferences.
try this Question: Android - How Do I Set A Preference In Code

How to store user state in Android in my case

I'm asking this question because I want to be sure which is the best way to store the user "logged in" / "logged out" state in my android application.
Here is what I am doing. In my application I connect to the server, send him username and password and if everything is ok it returns me OK. So now I want to be able to save the user logged in state all over my application because I need to change the content depends on that if user is logged in or not.
And the other thing is that I need to be able to save the user's login state, so the next time when he start the application he will be automatically logged in.
Any suggestions or ideas which is the best way to do that?
I made a little research over internet and find a few ways to do that :
MyApplication class which extends android.app.Application
SharedPreference
Or maybe a SQLite
There can be many reasons when your app looses focus, like pressing home button incoming call, etc but no matter what happens your onPause method of current Activity is called so what you have to is-
Override onPause method of every activity
Use SharedPreference to save the state of Application
I use static classes or SharedPreference - it'is easy and it's works.
SQLite it not good way (in my opinion)
your option SharedPreference is the perfect one in this condition as you need to store only boolean and you will access if often in your app to change the content.
The problem with storing the state on the device (using either the SharedPreferences or SQLite) is that the user can manipulate it. So if you simply store "isLoggedIn" in your SQLite-Database, this entry can be manipulated using dpms (for example).
So if you need to store the state, you should use the Application-class (this can be manipulated, too, but it's harder to do so). If the user closes the application, you destroy the state-variable so he needs to do the login when the application is opened the next time.
For usability, you could store username and (if the user wants so) the password.
Also, if you have a web-service that you use to check if the users login-data is correct, why don't you use for example OAuth to authentify the user on the server and deliver the content from it? This would basically make your application a pure "client".
SharedPreference is the best option to access single value in terms of timing which i observe to be true as compared to sql lite.

Sharing an object between activities

I have a Weather app with four Activities. The main/launcher activity is 'invisible' using...
android:theme="#android:style/Theme.Translucent.NoTitleBar"`
...and is simply used to do a few checks (whether this is a new install, whether a network connection is available etc) before firing off one of the other Activities. The other Activities are UI-oriented - two simply display weather data pulled from a website and the third to provide a location 'picker' so the user can choose which area to show the weather for.
However, all four activities make use of a WeatherHelper object which basically does everything from checking for available SD card storage to maintaining preferences and pulling/formatting website pages.
So, my question(s)...what is the best way to have one instance of WeatherHelper which can be used by multiple activities and where/how are best to create it in my case?
I've been an OO programmer for a lot of years but I'm very new to Android and the design concepts - I've read a lot on the Android Developers site over the past weeks but I've stalled trying to decide on this.
Any ideas gratefully received.
I would store shared information in you Application object. Subclass this and add any extra initialization and data there. You can get your application using getApplication() from your activity, which you can cast to your specialized version and access the shared data.
I would also avoid launching the special startup activity if possible and do the work in your Application's onCreate() override.
Well, your question has been answered, but it seems like it would be much simpler to instantiate your WeatherHelper object in the onCreate() of the Activity that has the launcher intent, and make the WeatherHelper static.

Categories

Resources