Guys I have developed a small game and I want to save high score on same screen.
I thought it's easy to save the integer but the problem is that I have no idea where to start can anyone guide me if you want I can give you my code
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
to save data
Editor editor = sharedpreferences.edit();
editor.putInt("key", your score goes here);
editor.commit();
and than to retrive your score back
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
int highScore = sharedpreferences.getInt("key" , defaultValue);
There you go
Related
I want to save in store a position in my shared preferences, and can edit it, and open it when app starts, thanks, i have this:
//load shared preferecnes
SharedPreferences sharedpreferences = getSharedPreferences("MyPrefs",
Context.MODE_PRIVATE);
//note: here idk how to read the last value of position saved
//Log.v("lastposition", sharedPreferences );
// on edit preferences and save
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("position", New LatLang(30,40) );
editor.commit();
Thanks everyone :)
LAST EDIT CHANGES:
//load shared preferecnes
SharedPreferences sharedpreferences = getSharedPreferences(this,Context.MODE_PRIVATE);
// --> HERE IDK HOT LOAD LAST POSITIONS SAVED
// on edit preferences and save
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putDouble("position_lat", 30d );
editor.putDouble("position_lon", 40d);
editor.commit();
sharedPreferences.getString("position", new LatLng(30,40).toString());
if your application has no information saved at "position", it needs to load a default value instead. Here it is LatLng(30,40).
But you can not save complex objects such as LatLng. What you can do instead, is saving/loading latitude and longitude values:
//load shared preferecnes
SharedPreferences sharedpreferences = getSharedPreferences(this,Context.MODE_PRIVATE);
//note: here you load the saved latitude and longitude values into the variable with name "loaded_position":
LatLng loaded_position = new LatLng(0,0);
loaded_position.latitude= sharedpreferences.getFloat("position_lat", 15f);
loaded_position.longitude = sharedpreferences.getFloat("position_lon", 15f);
Log.v("lastposition", "loaded position: ("+loaded_position.latitude+","+loaded_position.longitude+")" );
// on edit preferences, save the LatLng Object:
SharedPreferences.Editor editor = sharedpreferences.edit();
LatLng currentPosition = new LatLng(30f,40f);
editor.putFloat("position_lat", (float) currentPosition.latitude );
editor.putFloat("position_lon", (float) currentPosition.longitude);
editor.commit();
This code, loads your by editor saved (30,40) into loaded_position. It should load 15,15 instead on your first start of the application, because 30,40 is not saved at that first start in your sharedPreferences.
You can look at this page please:
http://developer.android.com/training/basics/data-storage/shared-preferences.html
i am using shared preference in android but it return null,
i saw a lot of code example for this and i can't see any error in my code
SharedPreferences sp =this.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
Log.v("sharedpref",""+username+": "+password);
editor.putString("email", username);
editor.putString("pass",password);
editor.apply();
and here i am retrieving the data from shared preference (in another activity)
SharedPreferences sp =this.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
Log.v("shersend",""+sp.getString("email", "Empty")+": "+sp.getString("pass", "Empty"));
String email = sp.getString("email", "Empty");
String pass = sp.getString("pass", "Empty");
so is there any problem with my code?
and is there a better to write this?
You are saving the preference in one Activity say A, And accessing in another activity say B, and context of both activities are different and so preferences values cannot be accessed as mode is private.
try
this.getApplicationContext().getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
Please try that way, Change this code block
SharedPreferences sp =this.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
to this code block
SharedPreferences sp= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Use getApplicationContext()
SharedPreferences sp =getApplicationContext().getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
Log.v("sharedpref",""+username+": "+password);
editor.putString("email", username);
editor.putString("pass",password);
editor.commit();
I am using following code:
SharedPreferences sharedPref = getSharedPreferences(GlobalDefines.SHARED_PREFERENCES, Context.MODE_PRIVATE);
String test = sharedPref.getString(GlobalDefines.GCM_KEY, "");
SharedPreferences.Editor editor = sharedPref.edit();
editor.clear();
editor.putBoolean(GlobalDefines.USER_IS_LOGGED_IN, false);
editor.remove(GlobalDefines.USER_NAME);
editor.remove(GlobalDefines.USER_PASSWORD);
editor.commit();
test = sharedPref.getString(GlobalDefines.GCM_KEY, "");
The string "test" has a value when I get the value from the shared preferences for the first time; when I remove another value from the preferences and want to get the same value (GCM_KEY) again, it is returned empty.
Why is that?
editor.clear() tells the editor that you want to remove ALL values from your SharedPreferences. Remove this line and you will see the expected behavior.
I want to save a time from a TimePickerDialog to sharedpreferences from my settings menu. I then want to retrieve this data when from another fragment. The time is stored as a long.
In the setting menu - when the positive button is pressed
SharedPreferences preferences = context.getSharedPreferences("TIME", Context.MODE_PRIVATE);
SharedPreferences pref = context.getSharedPreferences(
"any_prefname", Context.MODE_PRIVATE);
Editor editor = pref.edit();
editor.putLong("key_name", 8);
editor.commit();
In the fragment:
SharedPreferences pref = getActivity().getSharedPreferences(
"any_prefname", Context.MODE_PRIVATE);
Long longValue = pref.getLong("key_name", 0);
Toast.makeText(getActivity(), "Hi " + longValue, Toast.LENGTH_SHORT).show();
The problem is that the value "8" that I saved is note being shown in the toast from the fragment. The value being used is the 0.
Thank you
You aren't using the same key. When saving you used "time", when loading you used "key_name". You need to use 1 name.
i need to store a variable in phone memory, and later get that variable to show it.
Please, give me some information or anything to search. Thanks
static SharedPreferences settings;
static SharedPreferences.Editor editor;
settings = this.getPreferences(MODE_WORLD_WRITEABLE);
editor = settings.edit();
editor.putString("Variablenname_1", "1");
editor.commit();
if you want so get the value use:
String val = settings.getString("Variablenname_1", "0");
I think Shared Preferences is a good idea for you, read this for more information: http://developer.android.com/guide/topics/data/data-storage.html
The final result.
SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
String text = app_preferences.getString("name", "default");
...
SharedPreferences.Editor editor = app_preferences.edit();
editor.putString("name",name.getText().toString());
editor.commit();