我将尝试解释Android中的SharedPreferences。我是一位初学者,对此并不了解太多。
下面是我为我的应用程序实现的Preferences类。
事实上,我想设置默认偏好。它们将在应用程序安装时设置,并可以在应用程序中进行修改并保持持久性。 我听说过preferences.xml,但我不理解这个过程。
有人能帮助我吗?
谢谢您的时间。
下面是我为我的应用程序实现的Preferences类。
public class Preferences {
public static final String MY_PREF = "MyPreferences";
private SharedPreferences sharedPreferences;
private Editor editor;
public Preferences(Context context) {
this.sharedPreferences = context.getSharedPreferences(MY_PREF, 0);
this.editor = this.sharedPreferences.edit();
}
public void set(String key, String value) {
this.editor.putString(key, value);
this.editor.commit();
}
public String get(String key) {
return this.sharedPreferences.getString(key, null);
}
public void clear(String key) {
this.editor.remove(key);
this.editor.commit();
}
public void clear() {
this.editor.clear();
this.editor.commit();
}
}
事实上,我想设置默认偏好。它们将在应用程序安装时设置,并可以在应用程序中进行修改并保持持久性。 我听说过preferences.xml,但我不理解这个过程。
有人能帮助我吗?
谢谢您的时间。