标签:覆盖 events 字符 extends contex name protect data prot
版权声明:本文为博主原创文章,未经博主允许不得转载。
可以替代ACache用来保存用户名、密码。
相较于Acache,不存在使用猎豹清理大师进行垃圾清理的时候把缓存的数据清理掉的问题。
需要注意的是命名的KEY值直接在PreferencesUtils类中声明了。可以根据项目要求,在Globals类文件(或者在类似文件(用于存放全局变量和公共方法))中声明。
注意事项:
1、导入类文件后需要change包名以及重新import R文件路径
2、Values目录下的文件(strings.xml、dimens.xml、colors.xml等),如果项目中存在,则复制里面的内容,不要整个覆盖
package com.why.project.preferencesutilsdemo.utils; import android.content.Context; import android.content.SharedPreferences; /** * Used 临时存储数据操作类(全) */ public class PreferencesUtils { public static String PREFERENCE_NAME = "why"; /**用户名的key值*/ public static String USERNAME = "username"; /**存储字符串*/ public static boolean putString(Context context, String key, String value) { SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0); SharedPreferences.Editor editor = preferences.edit(); editor.putString(key, value); return editor.commit(); } /**读取字符串*/ public static String getString(Context context, String key) { return getString(context, key, null); } /**读取字符串(带默认值的)*/ public static String getString(Context context, String key, String defaultValue) { SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0); return preferences.getString(key, defaultValue); } /**存储整型数字*/ public static boolean putInt(Context context, String key, int value) { SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0); SharedPreferences.Editor editor = preferences.edit(); editor.putInt(key, value); return editor.commit(); } /**读取整型数字*/ public static int getInt(Context context, String key) { return getInt(context, key, -1); } /**读取整型数字(带默认值的)*/ public static int getInt(Context context, String key, int defaultValue) { SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0); return preferences.getInt(key, defaultValue); } /**存储长整型数字*/ public static boolean putLong(Context context, String key, long value) { SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0); SharedPreferences.Editor editor = preferences.edit(); editor.putLong(key, value); return editor.commit(); } /**读取长整型数字*/ public static long getLong(Context context, String key) { return getLong(context, key, 0xffffffff); } /**读取长整型数字(带默认值的)*/ public static long getLong(Context context, String key, long defaultValue) { SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0); return preferences.getLong(key, defaultValue); } /**存储Float数字*/ public static boolean putFloat(Context context, String key, float value) { SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0); SharedPreferences.Editor editor = preferences.edit(); editor.putFloat(key, value); return editor.commit(); } /**读取Float数字*/ public static float getFloat(Context context, String key) { return getFloat(context, key, -1.0f); } /**读取Float数字(带默认值的)*/ public static float getFloat(Context context, String key, float defaultValue) { SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0); return preferences.getFloat(key, defaultValue); } /**存储boolean类型数据*/ public static boolean putBoolean(Context context, String key, boolean value) { SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0); SharedPreferences.Editor editor = preferences.edit(); editor.putBoolean(key, value); return editor.commit(); } /**读取boolean类型数据*/ public static boolean getBoolean(Context context, String key) { return getBoolean(context, key, false); } /**读取boolean类型数据(带默认值的)*/ public static boolean getBoolean(Context context, String key, boolean defaultValue) { SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0); return preferences.getBoolean(key, defaultValue); } /**清除数据*/ public static boolean clearPreferences(Context context) { SharedPreferences pref = context.getSharedPreferences(PREFERENCE_NAME, 0); SharedPreferences.Editor editor = pref.edit(); editor.clear(); return editor.commit(); } }
package com.why.project.preferencesutilsdemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.why.project.preferencesutilsdemo.utils.PreferencesUtils; public class MainActivity extends AppCompatActivity { private EditText mUsernameEdt; private Button mLoginBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); initDatas(); initEvents(); } //初始化控件 private void initViews(){ mUsernameEdt = (EditText) findViewById(R.id.edt_username); mLoginBtn = (Button) findViewById(R.id.btn_login); } //初始化数据 private void initDatas(){ //判断是否缓存了用户名,如果是的话,读取缓存的用户名并填充到输入框中 initNamePwdFromCache(); } //初始化事件 private void initEvents(){ mLoginBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String userName = mUsernameEdt.getText().toString(); //缓存用户名 PreferencesUtils.putString(MainActivity.this,PreferencesUtils.USERNAME,userName); Toast.makeText(MainActivity.this,"已缓存用户名:"+userName,Toast.LENGTH_SHORT).show(); } }); } /** * 从缓存中查询用户名是否保存,并加载用户名 * */ private void initNamePwdFromCache() { //有缓存文件 String userNameCache = PreferencesUtils.getString(MainActivity.this,PreferencesUtils.USERNAME); if (userNameCache != null) { mUsernameEdt.setText(userNameCache); Toast.makeText(MainActivity.this,"已加载缓存的用户名:"+userNameCache,Toast.LENGTH_SHORT).show(); } } }
无
暂时空缺
https://github.com/haiyuKing/PreferencesUtilsDemo
PreferencesUtils【SharedPreferences操作工具类】
标签:覆盖 events 字符 extends contex name protect data prot
原文地址:http://www.cnblogs.com/whycxb/p/7026666.html