如何保存在Android的用户状态? [英] How to save user state in Android?

查看:190
本文介绍了如何保存在Android的用户状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的诺基亚手机的应用程序。当我安装该软件,并尝试使用它的第一次,它要求我输入密码并确认该密码。然后写它,然后点击【确定】按钮后,它可以让我使用的应用程序。

I have an application for my Nokia mobile. When I install that software and try to use it for the first time, it asks me for a password and to confirm that password. Then after writing it and clicking on OK button, it allows me to use the application.

现在,如果我关闭该软件,然后再次打开,它要求我只写密码。正确输入密码后,它可以让我再次使用它的应用。另外,如果我完全从手机的应用程序,并重新安装,它要求我输入密码并确认密码。

Now if I close that software and open again, it asks me to write ONLY the password. After entering the password correctly, it allows me to use its application again. Also if I totally remove that application from the phone and install it again, it asks me to enter the password and confirm the password.

那么告诉我,我怎么写这样的应用程序在Android中?

So tell me how do I write an application like this in Android?

推荐答案

据我了解的问题是如何实现的应用节约了用户状态。如果是这样,你可以使用共享preferences(esspecially如果你只有一个用户状态)或数据库。如果你启动应用程序,冷杉一次搜索共享preferances或DB数据。如果没有信息的应用程序会要求用户输入并确认密码后,其将数据写入到SP或分贝。

As far as I understand the question is how to realize saving a "user state" in application. If so, you can use SharedPreferences (esspecially if you have only one user state) or database. If you launch app firs time it search data in shared preferances or db. If there is no information app asks user to enter and confirm password after that it writes data to SP or db.

preTY simpe例如如何使用SP。 写SP:

Prety simpe example how to use SP. Writing to SP:

    SharedPreferences mySharedPreferences = 
            context.getSharedPreferences("PASSWORD_PREFS", Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = mySharedPreferences.edit();

    editor.putString("PSSWORD", password);
    editor.commit();

阅读SP:

    SharedPreferences mySharedPreferences = context.getSharedPreferences(PREFS, Activity.MODE_PRIVATE);
    String password = mySharedPreferences.getString(PASSWORD, null);

您可以使用edit.apply()insted的承诺的()。建议由Android团队。但是,这种方法对于API级别9.你可以在这里阅读有关的http://developer.android.com/reference/android/content/Shared$p$pferences.Editor.html#apply()

You can use edit.apply() insted of commit(). It is recommended by Android team. But this method is for API Level 9. You can read about it here http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#apply()

这篇关于如何保存在Android的用户状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆