I want to store a time value and need to retrieve and edit it. How can I use SharedPreferences
to do this?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Basic idea of SharedPreferences is to store things on XML file.
Declare your xml file path.(if you don't have this file, Android will create it. If you have this file, Android will access it.)
Write value to Shared Preferences
the
preference_file_key
is the name of shared preference files. And the1010101
is the value you need to store.apply()
at last is to save the changes. If you get error fromapply()
, change it tocommit()
. So this alternative sentence isRead from Shared Preferences
lsp
will be-1
ifpreference_file_key
has no value. If 'preference_file_key' has a value, it will return the value of this.The whole code for writing is
The code for reading is
I wanted to add here that most of the snippets for this question will have something like MODE_PRIVATE when using SharedPreferences. Well, MODE_PRIVATE means that whatever you write into this shared preference can only be read by your application only.
Whatever key you pass to getSharedPreferences() method, android creates a file with that name and stores the preference data into it. Also remember that getSharedPreferences() is supposed to be used when you intend to have multiple preference files for your application. If you intend to use single preference file and store all key-value pairs into it then use the getSharedPreference() method. Its weird why everyone (including myself) simply uses getSharedPreferences() flavor without even understanding the difference between the above two.
The following video tutorial should help https://www.youtube.com/watch?v=2PcAQ1NBy98
To obtain shared preferences, use the following method In your activity:
To read preferences:
To edit and save preferences
The android sdk's sample directory contains an example of retrieving and storing shared preferences. Its located in the:
Edit==>
I noticed, it is important to write difference between
commit()
andapply()
here as well.commit()
returntrue
if value saved successfully otherwisefalse
. It save values to SharedPreferences synchronously.apply()
was added in 2.3 and doesn't return any value either on success or failure. It saves values to SharedPreferences immediately but starts an asynchronous commit. More detail is here.Singleton Shared Preferences Class. it may help for others in future.
Simply call SharedPref.init() on MainActivity once
To Write data
To Read Data
Here i have created an Helper class to use preferences in android.
This is the helper class:
Setting values in Preference:
Retrieve data from preference:
more info:
Using Shared Preferences
Shared Preferences