- Создайте открытый класс SharedPreference Manager и класс Keystring, который будет содержать несколько данных, таких как имя, имя пользователя, пароль, номер телефона.
- Затем создайте объект этого класса в любом месте проекта
Получить и поместить значение легко в любом месте
SharedPreferenceManager ##
import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;
открытый класс SharedPreferenceManager {
protected Context mContext;
protected SharedPreferences mSettings;
protected Editor mEditor;
public SharedPreferenceManager(Context ctx, String prefFileName) {
mContext = ctx;
mSettings = mContext.getSharedPreferences(prefFileName,
Context.MODE_PRIVATE);
mEditor = mSettings.edit();
}
/***
* Set a value for the key
****/
public void setValue(String key, String value) {
mEditor.putString(key, value);
mEditor.commit();
}
/***
* Set a value for the key
****/
public void setValue(String key, int value) {
mEditor.putInt(key, value);
mEditor.commit();
}
/***
* Set a value for the key
****/
public void setValue(String key, double value) {
setValue(key, Double.toString(value));
}
/***
* Set a value for the key
****/
public void setValue(String key, long value) {
mEditor.putLong(key, value);
mEditor.commit();
}
/****
* Gets the value from the settings stored natively on the device.
*
* @param defaultValue Default value for the key, if one is not found.
**/
public String getValue(String key, String defaultValue) {
return mSettings.getString(key, defaultValue);
}
public int getIntValue(String key, int defaultValue) {
return mSettings.getInt(key, defaultValue);
}
public long getLongValue(String key, long defaultValue) {
return mSettings.getLong(key, defaultValue);
}
/****
* Gets the value from the preferences stored natively on the device.
*
* @param defValue Default value for the key, if one is not found.
**/
public boolean getValue(String key, boolean defValue) {
return mSettings.getBoolean(key, defValue);
}
public void setValue(String key, boolean value) {
mEditor.putBoolean(key, value);
mEditor.commit();
}
/**
* Clear all the preferences store in this {@link android.content.SharedPreferences.Editor}
*/
public boolean clear() {
try {
mEditor.clear().commit();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* Removes preference entry for the given key.
*
* @param key
*/
public void removeValue(String key) {
if (mEditor != null) {
mEditor.remove(key).commit();
}
}
}
KeyString
public class KeyString {
public static String PREFERENCE_NAME ="APP_PREF";
public static String NAME="NAME";
public static String USER_NAME="USER_NAME";
public static String PHONE_NUMBER="PHONE_NUMBER";
public static String PASSWORD="PASSWORD";
public static String USER_ID="USER_ID";
}
В любой деятельности в любом месте проекта
private SharedPreferenceManager preferenceManager=new new SharedPreferenceManager(context, KeyString.PREFERENCE_NAME);
Теперь установите некоторое значение
preferenceManager.setValue(KeyString.PHONE_NUMBER,"017xxxxx");
Получить данные из префсов
String phoneNumber=preferenceManager.getValue(KeyString.PHONE_NUMBER,""); //default value ""
ByЭтот метод вы можете установить и получить желаемое количество переменных и значений.просто добавьте в keyString и кодирование play.happy ..