Here is code I am Using to create and store value in Preference.
outgoing is String variable.
SharedPreferences sp = getSharedPreferences(outgoing, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("PhoneNo","Hi");
editor.commit();
Here is the code to get value from SharedPreference.
SharedPreferences sp
=getSharedPreferences(outgoing,Activity.MODE_PRIVATE);
String calln = sp.getString("PhoneNo","0");
Toast.makeText(mContext, "SHARED"+calln,Toast.LENGTH_LONG).show();
You should probably call the getSharedPreferences on the context from which you are accessing them.
Source
Therefore, depending on how you can access your context, if you pass it to some other activity or in an asynchronous task, here are some examples of usage:
this.getSharedPreferences(outgoing, Activity.MODE_PRIVATE);
context.getSharedPreferences(outgoing, Activity.MODE_PRIVATE);
getApplicationContext().getSharedPreferences(outgoing, Activity.MODE_PRIVATE);
Also, one way you can test your stuff is to use a listener when SharedPreferences get changed:
onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
Called when a shared preference is changed, added, or removed.
here is how to do that
You can also use the Preference Manager to obtain the SharedPreferences:
PreferenceManager.getSharedPreferences(YOUR_CONTEXT).getString(
"PhoneNo", "0");
Or to store them:
PreferenceManager.getSharedPreferences(YOUR_CONTEXT).edit().putString(
"PhoneNo", "Hi").commit();
The most likely reason that Shared Preference always returns the default value is that you saved the value in one preference file and then tried to retrieve it in another preference file. This can happen if you do call getPreferences() from different Activities, because getPreferences() creates a different preference file based on the activity it is created in.
Solution
The easiest solution is to always get your shared preferences like this:
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
This will use a single preference file for the entire app.
Alternate solution
If for some reason you need to use different preference files, then you can do
final static String PREF_FILE_1 = "pref_file_1";
...
SharedPreferences sharedPref = context.getSharedPreferences(PREF_FILE_1, Context.MODE_PRIVATE);
Just make sure you always use the right file name for the preferences you are trying to save and retrieve.
Local preferences
If you really only need a preference for a specific Activity, then you can use getPreferences(Context.MODE_PRIVATE). Just don't expect to be able to retrieve the values from another Activity in the same way.
See also
This answer describes the differences between the various ways of obtaining SharedPreferences.
Difference between getDefaultSharedPreferences and getSharedPreferences
Mess with the shared preferences of android - which function to use?
change this Activity.MODE_PRIVATE to this Activity.MODE_MULTI_PROCESS, issue is probably due to different context during storing value and accessing value.
When putting values, try changing this:
SharedPreferences sp = getSharedPreferences(outgoing, Activity.MODE_PRIVATE);
to this:
SharedPreferences sp = getApplicationContext().getSharedPreferences(outgoing, Activity.MODE_PRIVATE);
Same when getting values - don't forget to add getApplicationContext() in your call to SharedPreferences
EDIT:
Check that your "outgoing" String is the exact same in both Activities
Related
I want to utilize the sharedpreferences in my app, but in all of the tutorials and documentation in the internet I can not find out where to put the file that I am suppose to use.
What I mean is, when trying to access it as so:
public static final String PREFS_NAME = "AOP_PREFS";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
I don't know where to put the file AOP_PREFS.xml so that my class can use it. Am I missing something and I'm not suppose to be creating a file?
I don't quite understand what you are trying to do here. If your code is in an activity class, you just need to call getSharedPreferences(int) instead of getSharedPreferences(String, int). You get the preferences like this:
SharedPreferences prefs = getSharedPreferences (Context.MODE_PRIVATE);
Or you can use the PreferenceManager class to get the shared preferences as well:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
where this is a Context object.
You can retrieve a string from the preferences using a key (In this case, myString).
prefs.getString ("myString", "");
If you have not saved a string with the myString key, the second parameter will be returned.
This is how you can save a string into the preferences:
prefs.edit().putString ("myString", "Some String value");
The first parameter is the key, and the second is the value. Self-explanatory!
NOW REMEMBER! Every time you save something, call apply()!
prefs.edit().putString ("myString", "Some String value").apply();
*******
And that's all you have to know about shared preferences!
Using SharedPreferences, the file is automatically created in the files folder in internal storage of your app, so you don't need to explicitly do anything to create the file.
The file in this case will be AOP_PREFS.xml.
You can also just use "default" SharedPreferences, which will be stored in an xml file with the package name of your app.
Example of Writing to Default SharedPreferences:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sp.edit();
editor.putLong("some_value", someValue);
editor.commit();
Reading:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Long someVal = sp.getLong("some_value", 0);
Note that unless you have a rooted device, you won't be able to view files in internal storage.
So I have an application and i am trying to get one activity's shared preferences from a different activity. I cant use defaultSharedPreferences. Here is my code:
thisd = ma.
getSharedPreferences(
user, Context.MODE_PRIVATE);
Where thisd is the shared preferences, user is the specific sharedpreferences, and ma is the instance of the activity that has the shared preferences. Right now in activity called AddNameActivity.
Shared Preferences are not specific to any activity. It is common across application.
In the File where you add a value to Shared Prefernces, add this code sample.
SharedPreferences prefs = getSharedPreferences("your_file_name", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("yourStringName", "this_is_the_saved_value");
editor.commit();
In the file where you want to read the value (may be second activity), add this code sample.
SharedPreferences prefs = getSharedPreferences("your_file_name",MODE_PRIVATE);
String string = prefs.getString("yourStringName","default_value");
You can use a default file to save/ read your preferences.
In that case, Just replace the first line of the two code snippets above by this:
SharedPreferences prefs = getDefaultSharedPreferences(getApplicationContext());
I want to display the server response on multiple screen in header bar in Android? Please, tell me how to do that? I created an abstract class and defined the Asynctask class. In onPostExecute method i am using the textview for displaying the result. Now my question is how all the activities access this textview?
I am new with Android. Please, give me the proper way to solve this?
May be you can use the shared preference feature of android to save the response.
References are How to use SharedPreferences in Android to store, fetch and edit values and http://www.vogella.com/tutorials/AndroidFileBasedPersistence/article.html
On each activity, you can set the header by referring the value from shared preference.
Create one base activity and extend that created base activity instead of activity. Create one xml file in base activity, in it only declare your header. nothing else, now to display use that xml and for the other contents you can use your actual xml file. So this one is for to set global header. Now, save your response to Shared preferences and use it throughout the app whenever you need.
Store the value into Shared preferences
SharedPreferences sharedPref = context.getSharedPreferences("app_prefs", Context.MODE_PRIVATE);
// saving
sharedPref.edit().putString("key", "value").commit();
// reading
sharedPref.getString("key", "default");
You need to store the response values in Stringbuffer
StringBuffer value =new StringBuffer("your response value");
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("Identifier",value);
editor.commit(); //important, otherwise it wouldn't save.
Get the value in next activity
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);
final String myVariable = prefs.getString("Identifier", "value");
I am using a service to download and retrieve a list of URL's and put them in a sharedpreference.
With this..
SharedPreference images_article = this.getSharedPreferences("images_articles", MODE_WORLD_READABLE);
editor.putString("article2", urlImage2);
editor.putString("article3", urlImage2);
editor.commit();
Then in my Main.Activity i pull the url's from preference.
SharedPreference images_article = this.getSharedPreferences("images_articles", MODE_WORLD_READABLE);
urlImage2 = images_article.getString("article2", "NO ARTICLE AVAILABLE");
urlImage3 = images_article.getString("article3", "NO ARTICLE URL AVAILABLE");
The only problem is for some reason it isnt going inside of the shared preference, because the Main activity is loading the OLD URL's that has now changed. But in the Service i log the url's being retrieved and they are updated but for some reason in the main activity it still loads the old one. and im retrieving them from the same preference.
Is there anything i am missing or a better way to do this? Any help would be great!!
I was running in to a similar issue with my SharedPreferences between my Activity and Service. I ended up not using the default and used my own set file name
in the activity and service I set
private static final String PREFERENCE_NAME = "MyPreferenceFileName";
Then to get the values:
SharedPreferences pref = getSharedPreferences(PREFERENCE_NAME, Activity.MODE_PRIVATE);
pref_checked = pref.getBoolean("checked", true);
and to set the values:
SharedPreferences pref = getSharedPreferences(PREFERENCE_NAME, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("checked", value);
editor.commit();
This allowed me to use the same get and put logic in both my Service and Activity without any issues. I hope this helped.
This might be happening due to the different contexts you are accessing from. I am not very sure though, but you can try this :
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sp.edit();
Use this code, whenever you try to access SharedPreferences, that is, from both the Service and the Activity. That might solve your problem.
Using this in both the Activity and Service classes seems to work for me, as it should now be using the same Context to access the application SharedPreferences:
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Currently, I'm using the following code across all of my activities in my app to store application level variables and carry values between activities..
prefs = this.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
I didn't have a PreferenceActivity prior to this, but now I do and I am looking to store a few user prefs from this new PreferenceActivity in the same sharedPreferences tag, "MyPrefs".
I know I can access the PreferenceActivity SharedPrefs from my activities via
prefs = PreferenceManager.getDefaultSharedPreferences(this);
but I would like those values saved to my current sharedPreferences tag, "MyPrefs", but I'm not sure how to do this.
Thanks in advance..
You can change the default name of SharedPreferences file used by PreferenceActivity.
I did this in onCreate method of PreferenceActivity by adding the following piece of code:
getPreferenceManager().setSharedPreferencesName("MyPrefs");
Yes, you can http://idlesun.wordpress.com/2011/04/08/how-to-make-preferenceactivity-use-non-default-sharedpreferences/
You can't. the PreferneceManager always uses this.getPackageName () + "_preferences as preference name. Sorry to break the bad news. This is also important when you wish to use the new backup framework.
You can of course replace MyPrefs with this.getPackageName () + "_preferences.