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).
Related
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.
I'm worried about android security. I am storing the user id in sharedpreferences. I see some programs online that allow you to get into the sharedpreferences if your device is rooted... etc...
How do I prevent my sharedpreferences from being changed?
There is no way to avoid a user being able to change shared prefs. You need to implement security on your back end with session tokens so that even if the front end userid is changed the back end doesn't allow you to make requests because the session token doesnt match the user id. I assume you are asking because you are having you app communicate with a server and dont want the user to be able to see other peoples data. if not may i ask why you need to be able to do this?
read more about them here
Session token - how does it work?
Code like this
SharedPreference mySP = PreferenceManager.gerDefaultSharedPreference(this, Context.MODE_PRIVATE);
Only your app can access this...
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
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
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.