java 将少量信息保存为 android 中的设置(例如第一次运行该应用程序)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15385117/
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
Save little information as setting in android (like first time that app is run)
提问by Fcoder
I want to save a flag for recognizing that my app is run for the first time or not. For this simple job I don't want to create database..
我想保存一个标志来识别我的应用程序是否第一次运行。对于这个简单的工作,我不想创建数据库..
Is there a simple option to do this? I want to save and read little pieces of information only.
有没有一个简单的选择来做到这一点?我只想保存和阅读少量信息。
回答by Ajay S
Use sharedPreference
or files
to save the data but better option is sharedPreference
.
使用sharedPreference
或files
来保存数据,但更好的选择是sharedPreference
。
For Retrieving
用于检索
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean silent = settings.getBoolean("silentMode", false);
For Saving
为了节省
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("silentMode", true);
editor.commit();
回答by Rajitha Siriwardena
Use SharedPreferences
.
使用SharedPreferences
.
SharedPreferences preferences = getSharedPreferences("prefName", MODE_PRIVATE);
SharedPreferences.Editor edit= preferences.edit();
edit.putBoolean("isFirstRun", false);
edit.commit();
回答by Joop
A proper way to do this is by using the Android class SharedPreferences
which is used for things like this.
执行此操作的正确方法SharedPreferences
是使用用于此类操作的 Android 类。
Storing Settings
存储设置
SharedPreferences settings = getSharedPreferences(NAME_OF_PREFERENCES, MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("appPreviouslyStarted", true);
editor.apply();
Don't forget to apply or your mutations to the settings won't be saved!
不要忘记应用,否则您对设置的更改将不会被保存!
You can create multiple settings by using different NAME_OF_PREFERENCES
. The settings are stored on the device so will be available after closing the application.
您可以使用不同的NAME_OF_PREFERENCES
. 设置存储在设备上,因此在关闭应用程序后即可使用。
When you try to retrieve NAME_OF_PREFERENCES
that is not already created, you create a new one. See more behavior like this here.
当您尝试检索NAME_OF_PREFERENCES
尚未创建的内容时,您会创建一个新内容。在此处查看更多此类行为。
apply() versus commit()
apply() 与 commit()
You can use editor.apply()
as well as editor.commit()
, the only difference is that apply() does not return a boolean
value with if the edit was successful or not. editor.apply() is therefor faster and more commonly used.
您可以使用editor.apply()
as well editor.commit()
,唯一的区别是boolean
如果编辑成功与否, apply() 不会返回值。因此 editor.apply() 更快,更常用。
What is MODE_PRIVATE
什么是 MODE_PRIVATE
You can see all about the different modes here. For your case MODE_PRIVATE
is fine.
您可以在此处查看有关不同模式的所有信息。因为你的情况MODE_PRIVATE
很好。
Retrieving settings
检索设置
SharedPreferences settings = getSharedPreferences(NAME_OF_PREFERENCES, MODE_PRIVATE);
boolean silent = settings.getBoolean("silentMode", false);
When retrieving a settings from a SharedPreferences
object you always have to specify a default value which will be returned when the setting was not found. In this case that's false
.
从SharedPreferences
对象中检索设置时,您始终必须指定一个默认值,该值将在未找到该设置时返回。在这种情况下,就是false
.
回答by Raynold
I suggest you to go for SharedPreferencepersistent storage. Its very easy and fast storing/retrival for small amount of information.
我建议您使用SharedPreference持久存储。它非常容易和快速地存储/检索少量信息。
See the code to get the value from SharedPreference
查看代码以从 SharedPreference 获取值
// Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean silent = settings.getBoolean("silentMode", false);
setSilent(silent);
and to Store value in SharedPreference
并在 SharedPreference 中存储值
// 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);
回答by Alex Zaraos
You can do one class for example: (like a object for instance)
例如,您可以执行一个类:(例如一个对象)
import android.content.Context;
import android.content.SharedPreferences;
public class SettingsMain {
Context context;
SharedPreferences preferences;
SharedPreferences.Editor editor;
private static final String PREFER_NAME = "settingsMain";
public static final String KEY_VIBRATE = "switchVibrate";
public SettingsMain(Context context) {
this.context = context;
setPreferences();
}
private void setPreferences(){
preferences = context.getSharedPreferences(PREFER_NAME, context.MODE_PRIVATE);
editor = preferences.edit();
}
public void cleanPreferences(){
editor.clear();
editor.commit();
}
public void setStatusVibrate(Boolean status){
editor.putBoolean(KEY_VIBRATE, status);
editor.commit();
}
public Boolean getstatusVibrate(){
return preferences.getBoolean(KEY_VIBRATE, true);
}
}
On your activity call:
在您的活动电话中:
public class Home extends AppCompatActivity {
private SettingsMain settings;
private SwitchCompat switchVibrate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.master);
setResources();
getSettings();
}
private void setResources(){
switchVibrate = (SwitchCompat) findViewById(R.id.master_main_body_vibrate_switch);
switchVibrate.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
settings.setStatusVibrate(isChecked);
}
});
}
private void getSettings(){
settings = new SettingsMain(this);
switchVibrate.setChecked(settings.getstatusVibrate());
}
}
回答by Jay Mayu
What about using static
variables globally?
static
全局使用变量怎么样?
You can do this as given in this tutorial. I know handling Content providers are unnecessary just to keep some flags.
您可以按照本教程中的说明执行此操作。我知道处理内容提供程序是不必要的,只是为了保留一些标志。
Else you can check out Shared Preferencesprovided by Android. Here's a good exampleto get started.
否则,您可以查看Android 提供的共享首选项。这是一个很好的入门示例。
This would be my recommendation.
这将是我的建议。