覆盖已保存的首选项
Overwritten saved preferences
我正在编写一个应用程序来模拟使用 GPS 信息对测量数据建模的环境监测仪器 (https://github.com/sickel/measem)。在此应用程序中,我希望用户能够在给定位置定义点源。此位置的纬度/经度将存储在共享首选项中。该点应通过在设置中输入纬度和经度(使用首选项 API)或通过在 gpslocation 中重新定义来定义。带有设置的部分工作正常,我也可以从 gps 中读取位置,但是一旦应用程序暂停,该数据集就会被首选项编辑器中的数据覆盖。
我存储值的代码是:
SharedPreferences sp=this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor ed=sp.edit();
ed.putString("Latitude",lat);
ed.putString("Longitude",lon);
ed.apply();
我也试过ed.commit();除了apply,但结果是一样的。如何使用我存储在此处的内容来更新从首选项编辑器中看到的内容?
我的 onPause 对首选项没有任何作用,它只是做一些其他的内务处理:
public void onPause() {
super.onPause();
timer.cancel();
timer.purge();
h2.removeCallbacks(run);
Button b = (Button)findViewById(R.id.button);
b.setText("start");
}
我正在通过此函数中的首选项设置值
private void readPrefs(){
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String ret=sharedPref.getString("Latitude", "1");
Double lat= Double.parseDouble(ret);
ret=sharedPref.getString("Longitude", "1");
Double lon= Double.parseDouble(ret);
there.setLatitude(lat);
there.setLongitude(lon);
}
这是从 onResume() 和 onActivityResult() 调用的。我将 SettingsFragment 用于设置菜单。
getPreferences
在 docs:
Retrieve a SharedPreferences object for accessing preferences that are
private to this activity. This simply calls the underlying
getSharedPreferences(String, int) method by passing in this activity's
class name as the preferences name.
由于您试图让他们参与不同的活动,我怀疑您实际上指的是两个不同的共享首选项,即两个不同的文件。因此,我建议您在获取和放置时使用相同的方法来获取 SharedPreferences
实例。 PreferenceManager.getDefaultSharedPreferences(context)
应该和 context.getPreferences("NAME", Context.MODE_PRIVATE)
一样好。如果选择第二个,我建议将名称保留为资源以确保其一致。
我正在编写一个应用程序来模拟使用 GPS 信息对测量数据建模的环境监测仪器 (https://github.com/sickel/measem)。在此应用程序中,我希望用户能够在给定位置定义点源。此位置的纬度/经度将存储在共享首选项中。该点应通过在设置中输入纬度和经度(使用首选项 API)或通过在 gpslocation 中重新定义来定义。带有设置的部分工作正常,我也可以从 gps 中读取位置,但是一旦应用程序暂停,该数据集就会被首选项编辑器中的数据覆盖。
我存储值的代码是:
SharedPreferences sp=this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor ed=sp.edit();
ed.putString("Latitude",lat);
ed.putString("Longitude",lon);
ed.apply();
我也试过ed.commit();除了apply,但结果是一样的。如何使用我存储在此处的内容来更新从首选项编辑器中看到的内容?
我的 onPause 对首选项没有任何作用,它只是做一些其他的内务处理:
public void onPause() {
super.onPause();
timer.cancel();
timer.purge();
h2.removeCallbacks(run);
Button b = (Button)findViewById(R.id.button);
b.setText("start");
}
我正在通过此函数中的首选项设置值
private void readPrefs(){
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String ret=sharedPref.getString("Latitude", "1");
Double lat= Double.parseDouble(ret);
ret=sharedPref.getString("Longitude", "1");
Double lon= Double.parseDouble(ret);
there.setLatitude(lat);
there.setLongitude(lon);
}
这是从 onResume() 和 onActivityResult() 调用的。我将 SettingsFragment 用于设置菜单。
getPreferences
在 docs:
Retrieve a SharedPreferences object for accessing preferences that are private to this activity. This simply calls the underlying getSharedPreferences(String, int) method by passing in this activity's class name as the preferences name.
由于您试图让他们参与不同的活动,我怀疑您实际上指的是两个不同的共享首选项,即两个不同的文件。因此,我建议您在获取和放置时使用相同的方法来获取 SharedPreferences
实例。 PreferenceManager.getDefaultSharedPreferences(context)
应该和 context.getPreferences("NAME", Context.MODE_PRIVATE)
一样好。如果选择第二个,我建议将名称保留为资源以确保其一致。