共享首选项android基本问题

发布于 2024-11-28 23:55:49 字数 1288 浏览 3 评论 0原文

我在用户第一次打开应用程序时保存用户名和密码,并将其存储在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不语却知心 2024-12-05 23:55:49

请注意,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()

薄暮涼年 2024-12-05 23:55:49

“getSharedPreferences”的文档说:

所需的首选项文件。如果此名称的首选项文件不存在,则当您检索编辑器 (SharedPreferences.edit()) 然后提交更改 (Editor.commit()) 时,将会创建该文件。

确保您使用相同的编辑器对于提交之前的所有写入,例如

Editor editor = mPref.edit();
editor.putString("username", username);
editor.putString("password", password);
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.

Editor editor = mPref.edit();
editor.putString("username", username);
editor.putString("password", password);
editor.commit();
站稳脚跟 2024-12-05 23:55:49

对首选项进行任何更改后,您需要调用编辑器的 commit 方法。这将保存首选项文件:

SharedPreferences.Editor dhjEditor = dhj.edit();
dhjEditor.putString("username", username.getText().toString());
dhjEditor.putString("password", password.getText().toString());
dhjEditor.commit();

You need to call the Editor's commit method after making any change to preferences. This will save the preferences file:

SharedPreferences.Editor dhjEditor = dhj.edit();
dhjEditor.putString("username", username.getText().toString());
dhjEditor.putString("password", password.getText().toString());
dhjEditor.commit();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文