在 Android 中检索 SharedPreferences
Retrieving SharedPreferences in Android
我正在构建我的第一个多 activity 应用程序。我在 Activity1 中有一些地理坐标由用户定义并保存到 SharedPreferences
:
// these strings are used when saving the users' preferred location
private static final String POINT_LATITUDE_KEY = "POINT_LATITUDE_KEY";
private static final String POINT_LONGITUDE_KEY = "POINT_LONGITUDE_KEY";
private static final String TAG = "Activity1";
// actually saves the coordinates to the preferences
private void saveCoordinatesInPreferences(float latitude, float longitude) {
SharedPreferences prefs =
this.getSharedPreferences(getClass().getSimpleName(),
Context.MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = prefs.edit();
prefsEditor.putFloat(POINT_LATITUDE_KEY, latitude);
prefsEditor.putFloat(POINT_LONGITUDE_KEY, longitude);
//Log.i(TAG, "latitude is: " + latitude);
//Log.i(TAG, "longitude is: " + longitude);
prefsEditor.commit();
}
然后Activity2需要使用这些SharedPreferences坐标。我无法检索它们。这是我为检索而编写的方法。我的变量 latitude
没有写入 log
.
private static final String TAG = "Activity2";
protected void getLatLongPref() {
// adapted from http://mrbool.com/android-shared-preferences-managing-files-using-internal-external-storage/30508
// accessed April 10, 2015
SharedPreferences pref = getApplicationContext().getSharedPreferences("POINT_LATITUDE_KEY", MODE_PRIVATE);
float latitudeUser = pref.getFloat("POINT_LATITUDE_KEY", 0); // getting users chosen latitude
Log.i(TAG, "latitude is: " + latitudeUser);
}
你认为我做错了什么?
像这样更改您的第一个代码片段:
private void saveCoordinatesInPreferences(float latitude, float longitude) {
SharedPreferences prefs =
this.getSharedPreferences("myPref", Context.MODE_PRIVATE);
[...]
}
...你的第二个像这样:
protected void getLatLongPref() {
SharedPreferences pref =
this.getSharedPreferences("myPref", Context.MODE_PRIVATE);
[...]
}
基本上,两种情况都必须相同。另见 this。
您为两个共享首选项使用了错误的上下文和首选项名称。把第一个改成这样:
SharedPreferences prefs = getApplicationContext().getSharedPreferences("POINT_LATITUDE_KEY",
Context.MODE_PRIVATE);
我正在构建我的第一个多 activity 应用程序。我在 Activity1 中有一些地理坐标由用户定义并保存到 SharedPreferences
:
// these strings are used when saving the users' preferred location
private static final String POINT_LATITUDE_KEY = "POINT_LATITUDE_KEY";
private static final String POINT_LONGITUDE_KEY = "POINT_LONGITUDE_KEY";
private static final String TAG = "Activity1";
// actually saves the coordinates to the preferences
private void saveCoordinatesInPreferences(float latitude, float longitude) {
SharedPreferences prefs =
this.getSharedPreferences(getClass().getSimpleName(),
Context.MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = prefs.edit();
prefsEditor.putFloat(POINT_LATITUDE_KEY, latitude);
prefsEditor.putFloat(POINT_LONGITUDE_KEY, longitude);
//Log.i(TAG, "latitude is: " + latitude);
//Log.i(TAG, "longitude is: " + longitude);
prefsEditor.commit();
}
然后Activity2需要使用这些SharedPreferences坐标。我无法检索它们。这是我为检索而编写的方法。我的变量 latitude
没有写入 log
.
private static final String TAG = "Activity2";
protected void getLatLongPref() {
// adapted from http://mrbool.com/android-shared-preferences-managing-files-using-internal-external-storage/30508
// accessed April 10, 2015
SharedPreferences pref = getApplicationContext().getSharedPreferences("POINT_LATITUDE_KEY", MODE_PRIVATE);
float latitudeUser = pref.getFloat("POINT_LATITUDE_KEY", 0); // getting users chosen latitude
Log.i(TAG, "latitude is: " + latitudeUser);
}
你认为我做错了什么?
像这样更改您的第一个代码片段:
private void saveCoordinatesInPreferences(float latitude, float longitude) {
SharedPreferences prefs =
this.getSharedPreferences("myPref", Context.MODE_PRIVATE);
[...]
}
...你的第二个像这样:
protected void getLatLongPref() {
SharedPreferences pref =
this.getSharedPreferences("myPref", Context.MODE_PRIVATE);
[...]
}
基本上,两种情况都必须相同。另见 this。
您为两个共享首选项使用了错误的上下文和首选项名称。把第一个改成这样:
SharedPreferences prefs = getApplicationContext().getSharedPreferences("POINT_LATITUDE_KEY",
Context.MODE_PRIVATE);