共享首选项android基本问题
我在用户第一次打开应用程序时保存用户名和密码,并将其存储在 SharedPreferences 对象中。我在他第二次输入时检查数据,如果数据不为空,那么我就进入了应用程序。我是这样做的:
private SharedPreferences dhj;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dhj = this.getSharedPreferences("DHJ", MODE_WORLD_READABLE);
if(dhj.getString("username", null) != null) {
setContentView(R.layout.main);
// do some stuff...
}
else {
setContentView(R.layout.login);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
loginButton = (Button) findViewById(R.id.loginButton);
loginButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor dhjEditor = dhj.edit();
dhjEditor.putString("username", username.getText().toString());
dhjEditor.putString("password", password.getText().toString());
setContentView(R.layout.main);
}
});
// do some other stuff...
}
}
但是每次我打开应用程序时,系统都会要求我输入用户名和密码。
我做错了什么?我怎样才能实现所需的功能?
谢谢。
I save a user's username and passwords the first time he opens the app and store it in a SharedPreferences object. I check for the data the second time he enters and if its not null, then I got into the app. Here is how I'm doing this:
private SharedPreferences dhj;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dhj = this.getSharedPreferences("DHJ", MODE_WORLD_READABLE);
if(dhj.getString("username", null) != null) {
setContentView(R.layout.main);
// do some stuff...
}
else {
setContentView(R.layout.login);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
loginButton = (Button) findViewById(R.id.loginButton);
loginButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor dhjEditor = dhj.edit();
dhjEditor.putString("username", username.getText().toString());
dhjEditor.putString("password", password.getText().toString());
setContentView(R.layout.main);
}
});
// do some other stuff...
}
}
But each time I open the app, I am being asked to enter the username and password.
What am I doing wrong? How can I achieve the desired functionality?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请注意,editor.commit() 函数是执行文件系统操作的同步函数。从主线程调用它(您的代码似乎在主线程中运行)可能 - 在不幸的情况下 - 抛出 ANR,因为文件系统操作可能会停止并从而阻塞主线程。
我会使用
editor.apply()
函数,因为它会立即更新共享首选项的内存缓存,然后创建一个工作线程并将值从那里写入您的共享首选项文件(工作线程不会阻塞主线程)。http://developer.android.com/reference/android/content /SharedPreferences.Editor.html#apply()
Note that the
editor.commit()
function is a synchronous function that performs a file system operation. Calling this from the main thread (your code seems to run in the main thread) might - in unfortunet situations - throw a ANR since file system operations might stall and thereby block the main thread.I would use the
editor.apply()
function instead, since it will immediately update the in-memory cache of your shared preferences and then create a worker thread and write the values to your shared preferences file from there (worker threads don't block the main thread).http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#apply()
“getSharedPreferences”的文档说:
所需的首选项文件。如果此名称的首选项文件不存在,则当您检索编辑器 (SharedPreferences.edit()) 然后提交更改 (Editor.commit()) 时,将会创建该文件。
确保您使用相同的编辑器对于提交之前的所有写入,例如
The doc of "getSharedPreferences" says:
Desired preferences file. If a preferences file by this name does not exist, it will be created when you retrieve an editor (SharedPreferences.edit()) and then commit changes (Editor.commit()).
Make sure you use the same editor for all writing before committing, e.g.
对首选项进行任何更改后,您需要调用编辑器的
commit
方法。这将保存首选项文件:You need to call the Editor's
commit
method after making any change to preferences. This will save the preferences file: