尝试将用户信息存储到共享首选项文件中。 Android
Trying to store user information into a shared prefereance file. Android
我正在尝试将用户信息存储到 android studio 中的共享首选项文件中,但它不会写入文件 right.If 然后我为每个要保留的变量创建单独的文件它工作得很好,但是当我尝试将它们全部放在一个文件中时,它只在两个变量上写入一个输入(不是我想要的 ~
)
不知道是不是我漏掉了什么。
这是我的共同偏好 class:
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, "");
有什么想法吗?
您使用空字符串作为两个值的键。
给他们起个名字,例如:
public static final String USERNAME = "username";
public static final String PASSWORD = "password";
这些不是默认值,它们是在存储值时用作首选项文件中值标签的字符串。
如果您担心安全,将密码存储在首选项文件中可能不是一个好主意,但这取决于密码的重要性。作为替代方案,请考虑使用 Android Keystore System.
我正在尝试将用户信息存储到 android studio 中的共享首选项文件中,但它不会写入文件 right.If 然后我为每个要保留的变量创建单独的文件它工作得很好,但是当我尝试将它们全部放在一个文件中时,它只在两个变量上写入一个输入(不是我想要的 ~
)
不知道是不是我漏掉了什么。
这是我的共同偏好 class:
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, "");
有什么想法吗?
您使用空字符串作为两个值的键。
给他们起个名字,例如:
public static final String USERNAME = "username";
public static final String PASSWORD = "password";
这些不是默认值,它们是在存储值时用作首选项文件中值标签的字符串。
如果您担心安全,将密码存储在首选项文件中可能不是一个好主意,但这取决于密码的重要性。作为替代方案,请考虑使用 Android Keystore System.