我正在尝试将用户信息存储到android studio中的共享首选项文件中,但是它没有正确写入文件。如果我为每个变量创建单个文件,我想保留它然后它工作得很好但是当我尝试把它们全部放在一个文件中,它只写两个变量的输入之一(不是我想要的~
)
我不知道我在这里遗失了什么。
这是我的共享偏好类:
public class MySharedPerference {
//how to use
/*
MYPERFERENCE.writeString(getApplicationContext(),MYPERFERENCE.VARIABLE, "Values you want to store");
****Use Preferences to Read value using:-****
MyData.readString(getApplicationContext(), MyData.USERNAME,"");
*/
//this is the name of ther perfecne
public static final String MYPERFERENCE = "myprefs";
@SuppressWarnings("deprecation")
public static final int MODE = Context.MODE_PRIVATE;
// this is the varialbe that you want to store
public static final String VARIABLE = "";
public static final String MYNAME = "";
public static final String MYRANK = "";
public static final String USERNAME = "";
public static final String PASSWORD = "";
public static final String MYPOINTS = "";
public static final String MYGCMTOKEN = "";
public static final String MYBARCODE = "";
public static final String MYCUSTOMERNUMBER = "";
public static void writeBoolean(Context context, String key, boolean value) {
getEditor(context).putBoolean(key, value).commit();
}
public static boolean readBoolean(Context context, String key,
boolean defValue) {
return getPreferences(context).getBoolean(key, defValue);
}
public static void writeInteger(Context context, String key, int value) {
getEditor(context).putInt(key, value).commit();
}
public static int readInteger(Context context, String key, int defValue) {
return getPreferences(context).getInt(key, defValue);
}
public static void writeString(Context context, String key, String value) {
getEditor(context).putString(key, value).commit();
}
public static String readString(Context context, String key, String defValue) {
return getPreferences(context).getString(key, defValue);
}
public static void writeFloat(Context context, String key, float value) {
getEditor(context).putFloat(key, value).commit();
}
public static float readFloat(Context context, String key, float defValue) {
return getPreferences(context).getFloat(key, defValue);
}
public static void writeLong(Context context, String key, long value) {
getEditor(context).putLong(key, value).commit();
}
public static long readLong(Context context, String key, long defValue) {
return getPreferences(context).getLong(key, defValue);
}
public static SharedPreferences getPreferences(Context context) {
return context.getSharedPreferences(MYPERFERENCE, MODE);
}
public static SharedPreferences.Editor getEditor(Context context) {
return getPreferences(context).edit();
}
}
以下是我写给他们的方式
Log.d("My user name " , userName);
Log.d("My Password " , passWord)
MySharedPerference.writeString(getApplicationContext(),MySharedPerference .USERNAME, userName);
MySharedPerference.writeString(getApplicationContext(),MySharedPerference.PASSWORD, passWord);
以下是我如何阅读它们:
String storedUser = MySharedPerference.readString(myActivity.getApplicationContext(),MySharedPerference.USERNAME, "");
String storedPass = MySharedPerference.readString(myActivity.getApplicationContext(), MySharedPerference.PASSWORD, "");
有什么想法吗?
答案 0 :(得分:1)
您使用空字符串作为两个值的键。
给他们一个名字,例如:
public static final String USERNAME = "username";
public static final String PASSWORD = "password";
这些不是默认值,它们是在存储值时用作首选项文件中值的标签的字符串。
如果您担心安全性,将密码存储在首选项文件中可能不是一个好主意,但这取决于密码的重要性。作为替代方案,请考虑使用Android Keystore System。