私が設定の保存に行ったことを共有したい、私はSharedPreferences
と呼ばれる機能でアンドロイドのビルドを使用します。
public static final String USER_DATA_FIELD_NAME_AUTHKEY = "new_AuthKey";
public static final String USER_DATA_FIELD_NAME_EMAIL = "new_Email";
public static final String USER_DATA_FIELD_NAME_PASSWORD = "new_Password";
public static final String USER_DATA_FIELD_NAME_BIRTHDATE = "new_Birthday";
public static final String USER_DATA_FIELD_NAME_PREFACTURE = "new_Prefacture";
public static final String USER_DATA_FIELD_NAME_BLOODTYPE = "new_BloodType";
public static final String USER_DATA_FIELD_NAME_BODYTYPE = "new_IdealBodyType";
public static final String USER_DATA_FIELD_NAME_DURATIONPERIOD = "new_Duration";
public static final String USER_DATA_FIELD_NAME_STABILITY = "new_Stability";
public static final String USER_DATA_FIELD_NAME_SKINTYPE = "new_Skin";
public static final String USER_DATA_FIELD_NAME_RELATIONSHIP = "new_Relationship";
public static final String USER_DATA_FIELD_NAME_HEIGHT = "new_Height";
public static final String USER_DATA_FIELD_NAME_WEIGHT = "new_Weight";
public static final String USER_DATA_FIELD_NAME_PHY_1 = "new_phy1";
public static final String USER_DATA_FIELD_NAME_PHY_2 = "new_phy2";
この使用:上記のコードで
package tv.gsgw.android.garusuta.model;
import id.flwi.util.ActivityUtil;
import tv.gsgw.android.garusuta.constant.DataConstant;
import android.content.Context;
public class UserModel implements DataConstant {
Context _context;
private String authKey = "";
private String email = "";
private String password = "";
private String birthdate = "";
private String prefacture = "";
private String bloodType = "";
private String idealBodyType = "";
private String duration = "";
private String stability = "";
private String skin = "";
private String relationship = "";
private String height = "";
private String weight = "";
private boolean registerDataChange = false;
public UserModel(Context context){
this._context = context;
loadFromPreferences();
}
public void loadFromPreferences(){
setAuthKey(ActivityUtil.getSharedPreferenceString(_context, USER_DATA_FIELD_NAME_AUTHKEY, ""));
setEmail(ActivityUtil.getSharedPreferenceString(_context, USER_DATA_FIELD_NAME_EMAIL, ""));
setPassword(ActivityUtil.getSharedPreferenceString(_context, USER_DATA_FIELD_NAME_PASSWORD, ""));
setBirthdate(ActivityUtil.getSharedPreferenceString(_context, USER_DATA_FIELD_NAME_BIRTHDATE, ""));
setPrefacture(ActivityUtil.getSharedPreferenceString(_context, USER_DATA_FIELD_NAME_PREFACTURE, ""));
setBloodType(ActivityUtil.getSharedPreferenceString(_context, USER_DATA_FIELD_NAME_BLOODTYPE, ""));
setIdealBodyType(ActivityUtil.getSharedPreferenceString(_context, USER_DATA_FIELD_NAME_BODYTYPE, ""));
setPeriod(ActivityUtil.getSharedPreferenceString(_context, USER_DATA_FIELD_NAME_DURATIONPERIOD, ""));
setStability(ActivityUtil.getSharedPreferenceString(_context, USER_DATA_FIELD_NAME_STABILITY, ""));
setSkin(ActivityUtil.getSharedPreferenceString(_context, USER_DATA_FIELD_NAME_SKINTYPE, ""));
setRelationship(ActivityUtil.getSharedPreferenceString(_context, USER_DATA_FIELD_NAME_RELATIONSHIP, ""));
setHeight(ActivityUtil.getSharedPreferenceString(_context, USER_DATA_FIELD_NAME_HEIGHT, ""));
setWeight(ActivityUtil.getSharedPreferenceString(_context, USER_DATA_FIELD_NAME_WEIGHT, ""));
setRegisterDataChange(ActivityUtil.getSharedPreferenceBoolean(_context, USER_DATA_FIELD_REGISTER_DATACHANGE, false));
}
public void saveIntoPreferences(){
saveIntoPreferences(true);
}
public void saveIntoPreferences(boolean datachanged){
ActivityUtil.setSharedPreference(_context, USER_DATA_FIELD_NAME_AUTHKEY, getAuthKey());
ActivityUtil.setSharedPreference(_context, USER_DATA_FIELD_NAME_EMAIL, getEmail());
ActivityUtil.setSharedPreference(_context, USER_DATA_FIELD_NAME_PASSWORD, getPassword());
ActivityUtil.setSharedPreference(_context, USER_DATA_FIELD_NAME_BIRTHDATE, getBirthdate());
ActivityUtil.setSharedPreference(_context, USER_DATA_FIELD_NAME_PREFACTURE, getPrefacture());
ActivityUtil.setSharedPreference(_context, USER_DATA_FIELD_NAME_BLOODTYPE, getBloodType());
ActivityUtil.setSharedPreference(_context, USER_DATA_FIELD_NAME_BODYTYPE, getIdealBodyType());
ActivityUtil.setSharedPreference(_context, USER_DATA_FIELD_NAME_DURATIONPERIOD, getPeriod());
ActivityUtil.setSharedPreference(_context, USER_DATA_FIELD_NAME_STABILITY, getStability());
ActivityUtil.setSharedPreference(_context, USER_DATA_FIELD_NAME_SKINTYPE, getSkin());
ActivityUtil.setSharedPreference(_context, USER_DATA_FIELD_NAME_RELATIONSHIP, getRelationship());
ActivityUtil.setSharedPreference(_context, USER_DATA_FIELD_NAME_HEIGHT, getHeight());
ActivityUtil.setSharedPreference(_context, USER_DATA_FIELD_NAME_WEIGHT, getWeight());
ActivityUtil.setSharedPreference(_context, USER_DATA_FIELD_REGISTER_DATACHANGE, datachanged);
}
public String getAuthKey() {
return authKey;
}
public void setAuthKey(String authKey) {
this.authKey = authKey;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getBirthdate() {
return birthdate;
}
public void setBirthdate(String birthdate) {
this.birthdate = birthdate;
}
public String getPrefacture() {
return prefacture;
}
public void setPrefacture(String prefacture) {
this.prefacture = prefacture;
}
public String getBloodType() {
return bloodType;
}
public void setBloodType(String bloodType) {
this.bloodType = bloodType;
}
public String getIdealBodyType() {
return idealBodyType;
}
public void setIdealBodyType(String idealBodyType) {
this.idealBodyType = idealBodyType;
}
public String getPeriod() {
return duration;
}
public void setPeriod(String period) {
this.duration = period;
}
public String getSkin() {
return skin;
}
public void setSkin(String skin) {
this.skin = skin;
}
public String getRelationship() {
return relationship;
}
public void setRelationship(String relationship) {
this.relationship = relationship;
}
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
public boolean isRegisterDataChange() {
return registerDataChange;
}
public void setRegisterDataChange(boolean registerDataChange) {
this.registerDataChange = registerDataChange;
}
public String getStability() {
return stability;
}
public void setStability(String stability) {
this.stability = stability;
}
public void setRegistrationChanged(boolean dataChange) {
this.registerDataChange = dataChange;
}
public String toString(){
String str = "";
str += "authKey: " + authKey + "\n";
str += "email: " + email + "\n";
str += "password: " + password + "\n";
str += "birthdate: " + birthdate + "\n";
str += "prefacture: " + prefacture + "\n";
str += "bloodType: " + bloodType + "\n";
str += "idealBodyType: " + idealBodyType + "\n";
str += "duration: " + duration + "\n";
str += "stability: " + stability + "\n";
str += "skin: " + skin + "\n";
str += "relationship: " + relationship + "\n";
str += "height: " + height + "\n";
str += "weight: " + weight + "\n";
str += "registerDataChange: " + (registerDataChange ? "true" : "false") + "\n";
return str;
}
}
DataConstant
は次のように一定の変数が含まれているだけのクラスです:簡単にアクセスするために、私はSharedPreferences
のラッパーはこのようなコードを持っているUserModel
と呼ばれる作成します
package id.flwi.util;
/**
* @author Arief Bayu Purwanto
*/
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Calendar;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
public class ActivityUtil {
public static final String PREFS_NAME = "____MyPrefsFile";
public static void debugExtra(Bundle extras){
if(extras != null) {
Log.i("Log", "printing all extras information");
java.util.Set<String> keys = extras.keySet();
java.util.Iterator<String> keyIterator = keys.iterator();
int index = 0;
while(keyIterator.hasNext()) {
Log.i("log", " extras #" + (++index) + ": " + keyIterator.next());
}
} else {
Log.i("Log", "empty extras");
}
}
public static boolean getSharedPreferenceBoolean(Context c, String preference){
SharedPreferences settings = c.getSharedPreferences(ActivityUtil.PREFS_NAME, 0);
return settings.getBoolean(preference, false);
}
public static boolean getSharedPreferenceBoolean(Context c, String preference, boolean defaultValue){
SharedPreferences settings = c.getSharedPreferences(ActivityUtil.PREFS_NAME, 0);
return settings.getBoolean(preference, defaultValue);
}
public static String getSharedPreferenceString(Context c, String preference){
SharedPreferences settings = c.getSharedPreferences(ActivityUtil.PREFS_NAME, 0);
return settings.getString(preference, "");
}
public static String getSharedPreferenceString(Context c, String preference, String defaultValue){
SharedPreferences settings = c.getSharedPreferences(ActivityUtil.PREFS_NAME, 0);
return settings.getString(preference, defaultValue);
}
public static int getSharedPreferenceInt(Context c, String preference, int defaultValue){
SharedPreferences settings = c.getSharedPreferences(ActivityUtil.PREFS_NAME, 0);
return settings.getInt(preference, defaultValue);
}
public static long getSharedPreferenceLong(Context c, String preference, long defaultValue){
SharedPreferences settings = c.getSharedPreferences(ActivityUtil.PREFS_NAME, 0);
return settings.getLong(preference, defaultValue);
}
public static void setSharedPreference(Context c, String preference, boolean prefValue){
SharedPreferences settings = c.getSharedPreferences(ActivityUtil.PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(preference, prefValue);
editor.commit();
}
public static void setSharedPreference(Context c, String preference, Long prefValue){
SharedPreferences settings = c.getSharedPreferences(ActivityUtil.PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putLong(preference, prefValue);
editor.commit();
}
public static void setSharedPreference(Context c, String preference, int prefValue){
SharedPreferences settings = c.getSharedPreferences(ActivityUtil.PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt(preference, prefValue);
editor.commit();
}
public static void setSharedPreference(Context c, String preference, String prefValue){
SharedPreferences settings = c.getSharedPreferences(ActivityUtil.PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(preference, prefValue);
editor.commit();
}
}
を、それがPRで使用:またSharedPreferences
が容易で作業を支援するために作成するユーティリティクラスエティシンプル:
UserModel sharedData = new UserModel(getApplicationContext());
をちょうどそれが関連付けられていますフィールドのgetter/setterメソッドを呼び出してデータを変更したり、取得するには::
データをロードするには、あなただけ呼び出す必要があり
sharedData.getPassword();
sharedData.setPassword("new password");
最後に、フィールドを変更した場合は、saveIntoPreferences
に電話することを忘れないでください。
sharedData.saveIntoPreferences();
あなたの問題を解決するために役立つことを願っています。
できればそれをjavaに格納します。あなたがする必要がある場合はconfファイルに格納します。 – ZhongYu
なぜインタフェースの設定キーを保存するのですか?ことは、XMLファイル(PreferenceScreen、PreferenceCategoryなど)で環境設定を定義できるということです。その場合、アンドロイドを定義する必要があります:キーの値とそこから我々はjavaクラスにアクセスすることはできません。 android:sameValueと同じです。 – Ewoks