我需要一个包含基本类型设置的对象,以便能够通过从对象调用加载或保存方法来加载和保存 Android 共享首选项。
我创建了一个名为 thePreferences.java 的类,这个类有一些原始字段。我在此类中创建了 2x 个方法来从 sharedPreferences 加载和保存此类。请参阅下面我的类(class)中使用的代码:
import android.content.Context;
import android.content.SharedPreferences;
import com.google.gson.Gson;
import com.google.gson.JsonSerializer;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import java.io.Serializable;
public class thePreferences extends AppCompatActivity implements Serializable {
private static final String PREFS_NAME = "thePreferences";
private static final String PREF_TEXT = "pref_text";
public static final String TAG = "WPC";
public boolean loggedIn = false;
public int deviceId = 0;
public int userId = 0;
public String email = "";
public String deviceAlias = "";
public thePreferences() {
}
public thePreferences(boolean loggedIn, int deviceId, int userId, String email, String deviceAlias) {
this.loggedIn = loggedIn;
this.deviceId = deviceId;
this.userId = userId;
this.email = email;
this.deviceAlias = deviceAlias;
}
public boolean loadThePreferences(Context c) {
SharedPreferences pref = c.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
if (pref.contains(PREF_TEXT)) {
String theJson = pref.getString(PREF_TEXT, null);
Log.i(TAG, "The JSON string loaded from Shared Preferences : " + theJson);
thePreferences p = new Gson().fromJson(theJson, thePreferences.class);
this.deviceAlias = (p.deviceAlias);
this.deviceId = (p.deviceId);
this.email = (p.email);
this.loggedIn = (p.loggedIn);
return true;
} else {
Log.i(TAG, "Could not load SharedPreferences, value does not exist.");
return false;
}
}
public static boolean saveThePreferences(Context c,thePreferences p){
Gson gson = new Gson();
String theJson = gson.toJson(p);
c.getSharedPreferences(PREFS_NAME,MODE_PRIVATE)
.edit()
.putString(PREF_TEXT,theJson)
.commit();
return true;
}
@Override
public String toString() {
return "thePreferences{" +
"loggedIn=" + this.loggedIn +
", deviceId=" + this.deviceId +
", userId=" + this.userId +
", email='" + this.email + '\'' +
", deviceAlias='" + this.deviceAlias + '\'' +
'}';
}
}
然后我尝试从另一个 Activity 中调用它来加载和保存首选项,如下所示:
contextOfApplication = getApplicationContext();
thePreferences preferencesObj = new thePreferences(true,12345,552441,"abc@123.com","theDeivceAlias");
preferencesObj.saveThePreferences(contextOfApplication,preferencesObj);
preferencesObj.loadThePreferences(contextOfApplication);
我无法让它通过 saveThePreferences() 方法执行。它不断崩溃并出现 StackOverflow 异常,我不明白为什么。”
有人可以告诉我是否有其他方法可以做到这一点,或者我哪里出错了吗?
提前致谢
最佳答案
首先,thePreferences 是一个类,因此按照惯例应该以大写字母开头。
接下来,您不应该尝试序列化整个 Activity 。相反,尝试将您需要的数据存储为原始类型,并将它们作为原始类型获取。将整个类保存为 json 字符串在这里没有意义。
一种方法:
创建一个新类,其唯一目的是加载和保存您需要的数据。 (就像您的 thePreferences,但使其成为一个不继承 Activity 的简单类)。
新类将首选项名称和键作为静态字符串保存。
使用上下文实例化新类并将共享首选项预加载为最终成员
为您需要的单个值提供加载和设置方法
在您的 Activity 中使用此类作为成员或方法语言环境变量
Ps.: 去掉gson和serializable的东西
编辑:如果您真的想将数据存储为 json 字符串,则 sharedPreferences 不是要使用的实用程序。我不建议这样做,但是您应该将数据发送到服务器或将其存储在外部数据存储(SD 卡等)上。
关于java - Android对象从方法中序列化自身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37810407/