Create One class with name AppPreferences
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| import android.app.Activity;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;public class AppPreferences { private static final String APP_SHARED_PREFS = "com.chirag.saga.password"; private SharedPreferences appSharedPrefs; private Editor prefsEditor; public AppPreferences(Context context) { this.appSharedPrefs = context.getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE); this.prefsEditor = appSharedPrefs.edit(); } public String getPassword() { return appSharedPrefs.getString("password", "0000"); } public void savePassword(String text) { prefsEditor.putString("password", text); prefsEditor.commit(); }} |
Then in your Activity...
1
2
| protected AppPreferences appPrefs;appPrefs = new AppPreferences(getApplicationContext()); |
and
1
2
3
| String someString = appPrefs.getPassword();orappPrefs.savePassword(someString); |
No comments:
Post a Comment