Android: Unbale to save boolean in SharedPreferences - android

Hey guys I am unable to save a boolean value using SharedPreferences. The value is ALWAYS true for some reasons. Here is how I save the value:
public static void setSharedPreference(Context ctx, String keyValue, boolean value){
SharedPreferences sp = ctx.getSharedPreferences(Constants._preferencesName, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean(keyValue,value);
editor.commit();
}
And this is how I get it back:
public static boolean getBooleanPreference(Context ctx, String keyValue){
boolean prefValue;
SharedPreferences sp = ctx.getSharedPreferences(Constants._preferencesName, ctx.MODE_PRIVATE);
prefValue = sp.getBoolean(keyValue, false);
return prefValue;
}
What is wrong?!

Your code is syntactically correct, but I suspect you are passing different Context while saving than you are passing while reading from prefs. This will result in accessing different shared preferences storage. This is especially easy to step on if you are doing your writes and reads in different activities and decide to pass this as context. Unless there's a reason for doing so then you most likely want to reach your preferences from anywhere in your app then use always application context instead (getApplicationContext()).

Everything is correct in your code.
The ONLY possibility of a mistake is when you are calling these methods. Please use getApplicationContext() while putting and retrieving data.
And please do a "Clear data" for the app and start with a clean SharedPreference.

Related

Getting data from sharedPreference to a specific activity

Is it possible to get a sharedprefence data from MainActivity_A and fetch it only to MainActivity_B and the other activities like MainActivity_C and MainActivity_D cannot access the data that has been fetched to MainActivity_B?
Well It totally depends on you wheather you wanna get that data or not..once you store data in shared preference with key then just get that data in the specific activity you want Thats It !!
For Example in Activity X you save the Shared Preference value like this
public static final String SHARED_PREFS = "sharedPrefs";
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS,MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("Key",Value);
Then in Activity A you want to use this value then you can do it like this
SharedPreferences pref;
pref = getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);
String Value = pref.getString("Key");
In This way the value will be stored in variable String
And if you don't want the value in other activity then just don't call it !!

Remember/Forget device function on android app

I am building an app which connects & then controls a device.
I am wanting to add a remember device function and forget device function so the user can stick to one device or switch.
I am new to android so any help on how to implement this would be great.
You can keep the information in SharedPreferences. If you are connecting to a Bluetooth device then you need to remember the Mac address of the device.
Store and retrieve the info using the following functions.
public static void addDataToSharedPrefsString(String key, String value,Context context) {
SharedPreferences.Editor sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(
context).edit();
sharedPreferences.putString(key, value);
sharedPreferences.commit();
}
public static String getDataFromSharedPrefString(String key,Context context) {
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(context);
String value = preferences.getString
(key, "");
return value;
}
Add to shared pref by
addDataToSharedPrefsString("macAddress", "MAC123q4e",context);
get the shared value by
getDataFromSharedPrefString("macAddress",context);
if you want to delete an existing item in shared pref, set the value null.
addDataToSharedPrefsString("key", null,context);
I hope this is your requirement and this solution would help you.

Is it possible to set the SharedPreference in the BroadcastReceiver?

I need to generate notification using BroadcastReceiver and need to save some data in the BroadcastReceiver class.
So, I used SharedPreference. But not able to save data inSharedPreference`.
SharedPreferences pref_date;
public static final String MyPREFERENCES = "MyPrefs";
pref_date = context.getSharedPreferences(MyPREFERENCES,context.MODE_PRIVATE);
_ed = pref_date.edit();
String t="hello";
_ed.putString(_date,t);
_ed.putString(flag,"0");
_ed.commit();
Yes, SharepPreference is just a kind of file that has key-value pairs to store and retrieve. You can access it from anywhere within the application. Even from the Services created by your application.
Hard to tell w/o looking into the source code, but most possibly you forgot to invoke the Editor's apply() or commit() method, like so:
mSharedPreferences.edit().putInt("some_int", 0).apply();
As per the documentation, this is how you should write to your SharedPreferences:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("yourKey", "stringToSave");
editor.commit();
And this is how you read:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
String defaultValue = "defaultValue";
String yourSavedString = sharedPref.getString("yourKey", defaultValue);
This is a working copy/paste example. Keep in mind that "yourKey" is used to identify your value, since SharedPreferences is key/value pair, so they have to match both when you read and write.

How to use one time domain name in android?

I have a RESTful web-service, I am retrieving a data to android device
So here My ip address/Doamin name may change like..
192.168.0.1 or1-255 etc or It may be www.stackexchange.com or www.stackoverflow.com
Like my data will be stored in 192.168.0.1/rst/api/login or www.stackoverflow.com/rest/api/sitemview
So to over come this I want to use domain name as One time when i install application
in my application I have other pages like Login,display list-view, Single ItemView.
So this domain name should stored in device and pass to other activities every time when I use.
I used shared preferences like
public static void savePreferences(Context ctx, String key, String value) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(ctx);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
so when they load
public static Object loadSavedPreferences(Context ctx, String key) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(ctx);
return sharedPreferences.getString(key, "");
}
So here My problem is that This one time value is not permanent when ever I force close or Restart devise that passing value is not working
Any suggetion
If your domain name fixed, use below code.
public class WS{
public static final String domain="http://blah.ws";
}
Access it in each activity
with
String apiPath=WS.domain+your_rest_path;

Getting application data using SharedPreferences in Xamarin.Android

I need to extract the application data using SharedPreferences in Xamarin.Android.
Here is what I have tried in android.
public static void SetAuthentication(bool authenticationValue)
{
var localSettings = Application.Context.GetSharedPreferences ("Hello", FileCreationMode.Private);
localSettings.Edit ().PutBoolean ("ValidUser", authenticationValue).Commit ();
}
public static bool GetAuthentication()
{
var retValue = false;
object value;
var localSettings = Application.Context.GetSharedPreferences ("Hello", FileCreationMode.Private);
localSettings.GetBoolean ("ValidUser", out value);
}
But somehow I feel this is not the right approach.
Any guidance is appreciated.
Thanks
On the Android platform, preferences are stored in a text file in the private folders of the Android application - there are 2 ways to access these SharedPreferences data files.
You can either use the "default" file for the application, or you can use a named file, using any name you want.
This second approach is what you have chosen to do by calling GetSharedPreferences with a filename.
I would suggest to just use the "default" preferences. This way has some advantages:
the code is simpler
the preferences can be accessed more easily in a PreferenceActivity later on
To do this, create your ISharedPreferences instance using:
ISharedPreferences localSettings =
PreferenceManager.GetDefaultSharedPreferences (mContext);
Once you have your "default" shared preferences in this manner, the rest of your code stays the same.
For more info, check out related questions:
How do I use SharedPreferences in Xamarin.Android?
save android app string value after close xamarin
I agree with Richard so just to be clear and complete I would do like this:
public static void SetAuthentication(Context ctx, bool authenticationValue)
{
ISharedPreferences pref = ctx.GetSharedPreferences("your_app_preferences", FileCreationMode.Private);
ISharedPreferencesEditor edit = pref.Edit();
edit.PutBoolean("ValidUser", authenticationValue);
return edit.Commit();
}
public static bool GetAuthentication(Context ctx)
{
ISharedPreferences pref = ctx.GetSharedPreferences("your_app_preferences", FileCreationMode.Private);
return pref.GetBoolean("ValidUser"), null);
}
I hope this will make you feel happier.

Categories

Resources