0

我正在开发一个应用程序,它有一个登录页面。我需要存储登录凭据。它可以在我的 strings.xml 文件中吗?因为听说Strings.xml不能在运行时修改。那么我可以在哪里存储数据,即用户详细信息或应用程序详细信息?

4

4 回答 4

3

您可以将登录信息存储在SharedPreferenceSqliteDatabase中。

SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("username", YOUR_USERNAME);
        editor.putString("password", YOUR_PASSWORD);
        editor.commit();

用于检索登录信息

SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
String username = prefs.getString("username", null);
String password = prefs.getString("password", null);

如果您需要更高的安全性,您可以使用SqliteDatabase使用SQLCipher

请通过链接。

于 2013-10-25T16:51:51.233 回答
1

Use Shared Preferences. Like so:

Create these methods for use, or just use the content inside of the methods whenever you want:

public String getUserName()
{
  SharedPreferences sp = getSharedPreferences("userNameAndPassword", 0);
  String str = sp.getString("userName","no userName created");
  return str;
}

public String getPassword()
{
  SharedPreferences sp = getSharedPreferences("userNameAndPassword", 0);
  String str = sp.getString("password","no password created");
  return str;
}

public void writeToUserNameAndPassword(String userName, String password)
{
  SharedPreferences.Editor pref = 
                           getSharedPreferences("userNameAndPassword",0).edit();
  pref.putString("userName", userName);
  pref.putString("password", password);
  pref.commit();
}

You could call them like this:

// their userName if "foo" and their password is "bar"
writeToUserNameAndPassword("foo", "bar");

if (getUserName().equals(inputUserName) && getPassword.equals(inputPassword))
{
   // they have the right userName and password
}
else if (getUserName().equals("no userName created")
         && getPassword().equals("no password created"))
{
   // these preference Strings for their userName/password have both not been created
}
else if (getUserName().equals("no userName created"))
{
   // this preference String for their userName has not been created, 
   // but the password has been
}
else if (getPassword().equals("no password created"))
{
   // this preference String for their password has not been created, 
   // but the userName has been
}
else
{
   // they entered the wrong userName and/or password
}

Some explanation (if needed):

"password" and "userName" are the 'key' Strings in the preference. So you reference those keys to obtain the String you put in there. It is a reference name for the String you put.

"userNameAndPassword" is the preference name. You use the preference name, "userNameAndPassword", to reference the preference you want to access.

"no password created" and "no userName created" are the Strings that the getString method will return if the preference doesn't have a String referenced to by "password" or "userName", meaning that it hasn't been created.

Another way to put it: they are the default values of the reference String. So if nothing has been put their instead, the method will return the default values. You have to set the default values.

So, for example, if no "password" String has been put into the "userNameAndPassword" preference (written to using putString), then the getPassword() method will return "no password created".

于 2013-10-25T18:21:09.013 回答
0

简而言之,您不能存储或更改strings.xml的内容

但是是的,正如用户 @amit 所说,您可以将这些值存储在 Shared Preferences 中

或者你可以使用SQLite 数据库来存储你想要学习 sqlite的东西

例如

用于设置值

SharedPreferences.Editor prefEditor = getPreferences(MODE_PRIVATE).edit();
prefEditor.putInt(LAUNCH_COUNT, 1); // you can have multiple put (values)
prefEditor.commit();
prefEditor.apply();

为了获得价值

SharedPreferences sp = getPreferences(MODE_PRIVATE);
int launchCount = sp.getInt(LAUNCH_COUNT, -1);
于 2013-10-25T16:55:39.680 回答
0

正如@Armit 之前提到的,您可以将数据存储在 SharedPreferences 中。请注意,它存储在一个简单的 XML 文件中,可以使用编辑器查看和修改。您至少应该对其进行加密,或者更好的是,根本不保存它。通常,您登录到服务器或站点,然后只保存返回令牌。您只需使用令牌再次连接,而不必以纯文本形式保存密码。

于 2013-10-25T16:59:16.393 回答