Android 在应用设置中保存用户信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10209814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Saving user information in app settings
提问by Streetboy
i am creating login in my app. User type his info i send it to web service to check and get his ID. Now i need to save it somewhere in my app that i could check if he had login.
我正在我的应用程序中创建登录。用户输入他的信息,我将其发送到网络服务以检查并获取他的 ID。现在我需要将它保存在我的应用程序中的某个位置,以便我可以检查他是否已登录。
So where this info should be saved ? I know that every app have it's settings so there should be this info ? Maybe someone could give me more info what and how this is doing.
那么这些信息应该保存在哪里?我知道每个应用程序都有它的设置,所以应该有这个信息?也许有人可以给我更多信息,这是做什么以及如何做的。
Thanks.
谢谢。
I found this post: What is the most appropriate way to store user settings in Android application
我找到了这篇文章: 在 Android 应用程序中存储用户设置的最合适方法是什么
I believe my question was duplicate. But it would be nice if you have something to share more.
我相信我的问题是重复的。但如果你有更多的东西可以分享,那就太好了。
回答by V.J.
First of all save user info like uname & password in SharedPreferences with this code.
首先使用此代码在 SharedPreferences 中保存用户信息,如 uname 和密码。
SharedPreferences settings = getSharedPreferences("UserInfo", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("Username",txtUname.getText().toString());
editor.putString("Password",txtPWD.getText().toString());
editor.commit();
and then get SharedPreferences from below code
然后从下面的代码中获取 SharedPreferences
SharedPreferences settings = getSharedPreferences("UserInfo", 0);
txtUname.setText(settings.getString("Username", "").toString());
txtPWD.setText(settings.getString("Password", "").toString());
回答by MKJParekh
Answer is Use Application Class or Shared Preferences
答案是使用应用程序类或共享首选项
I assume you can easily search for the Example of Application Class
, SharedPreferences
and SQLite Database
我假设您可以轻松搜索Application Class
,SharedPreferences
和SQLite Database
So I am mentioning Where Do I Use Which Options
所以我提到我在哪里使用哪些选项
Application Class
Application Class
When I got anything(var , array , list , objects) to store just only for the LifeTime of the Application..and As it got close (for Android Force Close) I use Application Class.
当我得到任何东西(var、array、list、objects)只为应用程序的生命周期存储时......并且当它接近时(对于Android强制关闭)我使用应用程序类。
SharedPreferences
SharedPreferences
I use this when I got some short information like id,token,username,flags and I want to store that as long as the application rest in the Device..and need to use those information everytime I start the Application..
当我获得一些简短的信息(如 id、token、username、flags)时,我会使用它,只要应用程序保留在设备中,我就想存储这些信息……并且每次启动应用程序时都需要使用这些信息。
SQLite Database
SQLite Database
When there is a bulk data in Record format (Row - Column) I used it to store in database so it becomes easy to store , retrive and manipulate.
当有记录格式(行 - 列)的批量数据时,我用它来存储在数据库中,以便于存储、检索和操作。
回答by ariefbayu
You can use SharedPreferencesfor this.
您可以为此使用SharedPreferences。
Some codes from references (taken from documentation):
参考文献中的一些代码(取自文档):
public class Calc extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
@Override
protected void onCreate(Bundle state){
super.onCreate(state);
. . .
// Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean silent = settings.getBoolean("silentMode", false);
setSilent(silent);
}
@Override
protected void onStop(){
super.onStop();
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("silentMode", mSilentMode);
// Commit the edits!
editor.commit();
}
}
回答by Rohit Jain
if user clear the application data from setting -> application manager -> your application -> clear data
如果用户从设置中清除应用程序数据 -> 应用程序管理器 -> 您的应用程序 -> 清除数据
then all data saved in shared preferences will get removed
那么所有保存在共享首选项中的数据都将被删除