SharedPreferences for general app preferences and user session identificators - android

I'm using SharedPreferences to save two kinds of preference information.
1) General app preferences like results per page, sessionTime, etc. which are accessible only once you're logged in.
2) A session identifier like the users name and surname. One can see the name on every activity/page so as to know the the session is ok.
Is it ok for me to use sharedPreferences with 2 separate string identifiers. One for general settings and another for session info and manage the usage of these setting through my global application class or is my approach totally wrong?

By design Android does not differ between application/user preferences. Don't bother creating 2 different preference files, just use PreferenceManager.getDefaultSharedPreferences(yourContext) everywhere

Related

Does Shared preferences get shared across user

I am having some question, kindly help me out, following are my questions?
Does shared preferences data get shared across users?
Also can we have same widget IDs in two different users?
I am having a widget which any user can use. I am getting some
conflicts when a guest user or a any new user is switched to. Specifically, my widget is resizeable and in order to save its state so that it can be inflated accordingly on phone restart or some refresh events. These are working perfectly for the Admin User but it behaves abnormally when a guest is added or if the user is switched.
Yes shared preferences data get shared across users if we define CONTEXT MODE while creating SahredPreference for more detail you can read here for detail, and here. For widget id i think we can give same id in different users but here (i am not sure) also should be concept of Mode.

Android user session and SharedPreferences

I'm currently working on an android application that fetches information in a webservice. Those informations are related to a user ( who has a numeric user ID ).
So basically, I do Volley requests on URLs like this one :
http://api.mydomain/user/items with a POST parameter which is the userID I want to retrieve items.
When the user first log in to my app, I actually store his user ID in the Shared Preferences. This is useful for two things :
I can access his ID from anywhere in my application and therefore do Volley request with the current user ID.
I also use it on the application Splashscreen ( I check if there is this value in the shared preferences file and if so, he doesnt have to log in again and he's redirected to the application main activity ).
When the user disconnects from the application, I clear the Shared Preferences file, so he will have to log in again next time he open the app.
So here are my problems :
If a rooted user open the shared preferences file, he will be able to change the user ID, and so his identity.
If i keep this operating mode, I can't store user settings preferences ( Enable notifications etc.. ), which is the purpose of the shared preferences file ( I believe ), because i clear it when user disconnects in order to ask him to log-in again next time.
So, as you may think, I have the feeling that it's not the right thing to do..
What is the best pratice to do what i want to do ?
Keep some kind of "session", to avoid the user having to reconnect each time he launch the app.
Store his user ID in a secure manner, to prevent a malicious user to change it.
Is there a way to encrypt those critical data ? Maybe shouldn't I use a numeric ID, but rather some kind of encrypted key, to make it more difficult to change ?
Are SharedPreferences really used to store user's settings ( Should'nt I rather store it in the database ? )
Thank you in advance for helping me and sorry for bad english.
SharePreferences are used to store such information. I store the Auth token in sharedPref in one of my apps. Yes, if user has rooted the phone, the sharedpreference xml becomes directly accessible but that's something that user has to take care off (since rooting is normally not advisable and if rooted don't handover the phone to a programmer!)
Coming to your scenario, you are deleting the shared preference on app close (disconnect). Seems like you are simply using the shared preference in one app life cycle and not the entire lifecycle (from install to uninstall). You can be better off by using a singleton class here. Singleton class will have one instance per life cycle and you can set the userId and get it from anywhere in the applifecycle!! :
Class mySingletonClass{
private String userID;
privat static mySingletonClass instance;
public static getInstance()
{
if(instance==null) instance = new mySingletonClass();
return instance;
}
//getter setter for userID
}
to set UserID:
mySingletonClass.getInstance().setUserID(String);
to ger UserID:
mySngletonClass.getInstance().getUserID();
Now even if the phone is rooted, the userID is safely residing in heap memory !
If you do want to user SharedPreference for whole app life cycle (install to uninstall) and are worried about phone being rooted and userId being stolen, you can obfuscate the id before storing, but then again you need to store Key in shared preference too which defeats the purpose. Another way can be to store the key on server. Cracker can still snoop in the network calls and access the key but its a long shot.

Android - How to get only my own SharedPreferences

My app can backup and restore all my data, including the shared preference values. However, when I get all my shared preferences, I also get two with the names "drt" and "drt_ts". I've discovered these are from Admob.
Is there any way for me to iterate through the preferences and get ONLY the one ones my own app has created? There doesn't seem to be any concept of owner or creator.
If you want to have your own SharedPreferences, that is separate from the Default one for your app you should consider using SharedPreferences.getSharedPreferences(String name, int mode). The BackupHandler has support for multiple SharedPreferences files.
If you want to have AdMob save their preferences somewhere else then that is a different question, maybe it is possible, check with the documentation for AdMob?
http://developer.android.com/reference/android/content/Context.html#getSharedPreferences%28java.lang.String,%20int%29

What is use of preference activity in android?

I have studied preference activity but I don't understand it. Can anybody tell me what is the use of preference activity? How does it differ from activity?
Thanks
PreferencesActivity is a way to easily create preference screens such as those in Android itself, just look under Settings. These can be used inside applications to easily save preferences to SharedPreferences and then easily access these from within your app.
See this page for more information on PreferenceActivity.
An example of a PreferenceActivity showing some preferences:
Use:
It gives a very easy way to create a standard looking settings screen for any application.
Why:
minimal coding is required
How it works:
it takes care of UI.The values are stored in SharedPreferences automatically by the PreferenceActivity and so its scope becomes app wide.
Sample Screen UI:
such settings screen, you can use it for your app where user want to change setting.
Its one stop to manage your Shared Preferences. Through this, you can provide a group of Settings to user for your app. Since it only deals with preferences, that's why its different from regular Activity. Read this for more info
PreferenceActivity supports the simple handling of preferences. It can show a set of preferences to the user.
An activity on the other hand is a single thing a user can do.
Checkout Section 13 at this link
It is usually used to create a settings screen for applications .The values are stored in SharedPreferences automatically as soon as user selects a preference setting. It frees the programmer from explicitly saving preference values.
PreferenceActivity is a class that allows you to implement an activity that realizes a hierarchical organization of choices. Most often it provides a good bases for building the settings activity in you application.

SharedPreferences on android

I have a problem with SharedPreferences and my PreferenceActivity. I need that my application can difference between differentes preferences and users, so every user has his own preferences. I thought in use SharedPreferences sending to it the user ID to get the correct options. The problem is that I dont know how to do for my PreferenceActivity use that specific options. Currently, It is using the context options, so when I restart my application, it loads the last options modifieds...
How can I configure my preferenceActivities to store his changes in the sharedpreferences that I want? And for the load?
Thanks!
It's unusual to support the concept of 'users' on Android - most apps assume a phone has a single user.
That said, you can create custom SharedPreferences like this
SharedPreferences userprefs = getSharedPreferences(username,MODE_WORLD_READABLE);
For your PreferenceActivity, in onCreate you do this
getPreferenceManager().setSharedPreferencesName(username);
Hope that helps...
This might be a bit late, but you can also check out Swarm's Cloud Data, which provides a per-user SharedPreferences system, stored in the cloud (so if the user switches devices, their preferences persist on the new device).

Categories

Resources