Im creating a alarm app that will have multiple alarm.. so i want to store the times in shared preference. if there is any solution to store integer list in shared preference
SharedPreferences preferences = getSharedPreferences("prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("key", yourIntValue);
editor.apply();
Use editor.apply() to save preferences asynchronously. apply() is preferable in the most cases.
Use editor.commit() to save preferences synchronously.
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 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".
I have an alarm notification with operating normally while the phone is connected, but it reiniciaá the prefrências are reset. How to save?
You have a number of choices for persisting information. The two most common are
Sqlite database
Using preferences.
Example to set a preference:
SharedPreferences prefs = context.getSharedPreferences(<Unique string>, Activity.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putBoolean(Strings.STATUS_INTERNET, isInternetConnected);
editor.putString(Strings.USER_NAME, userName);
editor.commit();
Example to get a preference:
isDefaultAccountEnabled = prefs.getBoolean(Strings.DEFAULT_ACCOUNT, true);
I'm using shared preferences throughout my application. I'm having a problem though. I think my data is being lost on background.
SharedPreferences preferences = getSharedPreferences(pref_data, Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("isDriverLogin", "True");
editor.putString("driverPassword", driverPassword);
editor.putString("carrierId", carrierId);
editor.putString("CCTID", cctid);
editor.putString("shipment", entityShipment);
editor.putString("isAccepted", "");
editor.commit();
And somewhere in the app I create another object and edit just one portion of the data
SharedPreferences preferences = getSharedPreferences(pref_data, Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("isDriverLogin", "True");
editor.commit();
I'm doing this throughout my application. Editing just one or two data. But at some point my app crashes or the expected behavior is not happening. This behaviors are dependent on my preference data.
I tried fixing it by just creating one object of share preferences and always edit them with all the data. For example I have 5 data on shared preferences. If I just need to edit one, I will still put some data on the other 4 just to make sure nothing is being overwritten.
Do you guys have any idea?