I saw this on stack Need to save a high score for an Android game
This is what it told me
//setting preferences
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putInt("key", score);
editor.commit();
//getting preferences
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
int score = prefs.getInt("key", 0); //0 is the default value
I was wondering, should "key" be the string where the highscore is located? and does it matter what i name my prefs key.
Thanks for your time.
The Android Developers documentation is an excellent place to start with any question like this. SharedPreferences can be found here. SharedPreferences is a simple Key-Value pair storage, so if you put an int into your shared preferences as with the key "high score", then in the future, if you want to get that int, you need to use prefs.getInt("high score"). It doesn't matter what you name your keys, but it is generally good practice to use private static final string variables to store keys you will use on a regular basis.
You can name the string whatever you like. Click here for documentation. You can name it anything from "foo" to "bar".
Related
How to secure SharedPreferences data in my application?.
I use 2 step for this but in Security Audit hacker are able to hack my data.
1 Step-
SharedPreferences sharedPreferences = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("key", value);
editor.commit();
2 Step- Use SecureSharePreferences
SecurePreferences securePrefs = new SecurePreferences(context, "key", "my_user_prefs.xml");
SharedPreferences.Editor editor =securePrefs.edit();
editor.putString(key, value);
editor.commit();
Any other method to handle this.
After read some Answer I update my code with encrypt data but problem still exist.Security auditor still getting application sharedpreference.file from app memory.
SharedPreferences sharedPreferences = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
String keyEncript = EncriptionDecriptionUtils.encriptionOfData(key).toString().trim().replaceAll("\r\n", "");
String value = sharedPreferences.getString(keyEncript, "").trim().replaceAll("\r\n", "");
String valuedecript = EncriptionDecriptionUtils.decriptionOfData(value).toString().trim().replaceAll("\r\n", "");
return valuedecript;
You can encrypt & decrypt the shared preferences data using AES algorithm.If you open shared preferences explicitly you will get encrypted information only.For your reference look into this Securing SharedPreferences Data using AES algorithm
On a rooted phone, it can access the shared preferences for your app. Also, on any phone the user can delete all the data that it's stored in shared preferences by clearing the cache in the application manager. A safe way to store data would be to encrypted it with AES and save it in a text file in the root folder of your app.
I just created a list view which consists of 2 list items as Colors and Temple. Colors and Temple again having a list items of Plans and Views. When I click on the Colors I should get the contents of Colors Plans and Colors Views. And when I click on the Temple I should get the contents of Temple Plans and Temple Views. How should I have to store the state and how can I get the contents using shared preferences.Please help me ..
SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. Each SharedPreferences file is managed by the framework and can be private or shared.
Follow the link of the official developer site for more:
https://developer.android.com/training/basics/data-storage/shared-preferences.html
You can write to Shared Preferences
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();
and then read from Shared Preferences
SharedPreferences sharedPref =
getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);
And also dont forget to get a handle
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
Hope this helps for now, do you need more support?
I am Wondering about One-time screens... I know, I should use something like SharedPreferences or stuff like that.
If someone has a simple solution for one-time login screen. And a little example.
My login contains: weight, name , height, age and gender (spinner)
You can take a look at Android User info and Sign in :
https://developer.android.com/training/sign-in/index.html
Or you can use login with Facebook API.
Otherwise, I would use Shared prefs.
Create a shared prefs file
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
This will create a private file for the current activity. You can use MODE_WORLD_READABLE and MODE_WORLD_WRITABLE if it fits your needs.
You can also provide a file name as the first parameter if needed :
SharedPreferences sharedPreferences = getPreferences("com.example.stackoverflow.myfile", Context.MODE_PRIVATE);
Write a shared pref
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("USERNAME", "test");
editor.commit();
You can put any primitive type : int, string, boolean, etc.
It is a key/value set. the key string "USERNAME" will then have a value of "test".
Read shared pref
String username = sharedPreferences.getString("USERNAME", "NO NAME");
The second parameter is a default value to use if the key "USERNAME" didn't get any value.
the Html parse is very slow, so I thought of storing a static text within the sharded preferences to access it faster. Is it possible to store this somehow and retrieve it, so that it can be set without usage of Html.fromHtml?
This way I would just parse the file one time. Once it is in cache it should be much faster, if no parsing is required.
Yes you can store Static string in shared preferences.
Here is code which may help you..
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(YOUR_KEY, YOUR_STATIC_STRING);
editor.commit();
i have almost completed my game and now i wish to do something with the scores and name so i want to have a high score list...
The problem is that i cannot retrieve the name when i m trying to draw the name using alphabet bitmaps what i have preprared!!!
now the problem occurs when i m tryin to use the getSharedpreferences and m not able to copy the name anywhere in the whole project so that when in the activity of showing the high score i can draw it!!!
is there a simple way to do it please suggest!!!!
thank you!!!
The best way to store a value which will be available all around your application is, as you said, the SharedPreferences.
To store your name user you can do this:
SharedPreferences mPrefs = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("username", "your name");
editor.commit();
To retrieve your username you can do this:
SharedPreferences mPrefs = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
String username = mPrefs.getString("username", "");