SharedPreferences使用

SharedPreferences使用,一般常用于保存程序的配置信息,如记住登陆账号等
SharedPreferences prefs = getSharedPreferences(NAME,
				Context.MODE_PRIVATE);
//保存数据
private void savePreference(String key, Object value) {
		SharedPreferences.Editor editor = prefs.edit();
		if (value instanceof String) {
			editor.putString(key, (String) value);
		}
		else if (value instanceof Boolean) {
			editor.putBoolean(key, (Boolean) value);
		}
		else {
			throw new RuntimeException("unsupported data type");
		}
		editor.commit();
	}
//取数据 
this.tel = prefs.getString("tel", null);
		this.pwd = prefs.getString("pwd", null);
		this.rememberMe = prefs.getBoolean("rememberMe", false);
		this.autoLogin = prefs.getBoolean("autoLogin", false);

你可能感兴趣的:(SharedPreferences使用)