今天我在Android中遇到了一个非常奇怪的问题。我正在使用 2 个具有共享偏好的字段。一是deviceId,二是Hashkey。现在首先我使用代码仅将 deviceId 存储在 sharedprefs 中
new SharedPref(Home.this);
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String deviceId = telephonyManager.getDeviceId();
MyApplicationGlobal.DEVICE_ID = deviceId;
Log.v("deviceId in getAndStoreDeviceId()", "deviceId: " + deviceId);
SharedPref.writeString(SharedPref.DEVICE_ID, deviceId);
然后我从共享首选项中读取 hashKey 并将其分配给我的全局应用程序类中的一个变量
if (MyApplicationGlobal.HASHKEY == null) {
Log.v("in setHashKey before", "MyApplicationGlobal.HASHKEY: " + MyApplicationGlobal.HASHKEY);
MyApplicationGlobal.HASHKEY = SharedPref.readString(SharedPref.KEY_HASH, null);
Log.v("in setHashKey after", "MyApplicationGlobal.HASHKEY: " + MyApplicationGlobal.HASHKEY);
}
但问题是当我第一次运行此代码时,共享首选项中 hasKey 的值与 deviceID(在模拟器上运行)的值相同,即
000000000000000
所以我首先卸载了应用程序然后删除并重新创建了一个新的模拟器,但是每次共享首选项中的hashkey的值都是一样的,都是000000000000000。那怎么可能,因为我想,我没有写值在共享首选项中,因此它应该为 null 或其他内容,但绝对不是 000000000000000
Log cat 上的输出是
12-14 19:55:21.733: V/deviceId in getAndStoreDeviceId()(969): deviceId: 000000000000000
12-14 19:55:21.753: V/in setHashKey before(969): MyApplicationGlobal.HASHKEY: null
12-14 19:55:21.753: V/in setHashKey after(969): MyApplicationGlobal.HASHKEY: 000000000000000
12-14 19:55:21.765: V/in UserFunctions() MyApplicationGlobal.Hask_KEY(969): MyApplicationGlobal.HASH_KEY: 000000000000000
12-14 19:55:21.813: V/in getListOfEventCountries() MyApplicationGlobal.HASH_KEY(969): MyApplicationGlobal.HASH_KEY: 000000000000000
我的 SharedPref 类的代码是
package com.library.shared_preference;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class SharedPref {
public static final String PREF_NAME = "SHARED_PREFERENCES_FOR_TRACKINT";
public static final String SEARCHED_IMG_URL = "SEARCHED_IMG_URL";
public static final String SEARCHED_IMG_NAME = "SEARCHED_IMG_NAME";
public static final String SEARCHED_IMG_ACTUAL_PRICE = "SEARCHED_IMG_ACTUAL_PRICE";
public static final String UID_RES = "u_id";
public static final String USER_EMAIL = "user_name";
public static final String USER_PWD = "user_pwd";
public static final String FB_UID_RES = "u_id";
public static final String FB_USER_EMAIL = "user_name";
public static final String FB_USER_PWD = "user_pwd";
public static final String DEVICE_ID = "";
public static final String KEY_HASH = "";
static Context _context;
// constructor
public SharedPref(Context c) {
_context = c;
}
// for boolean value
public static void writeBoolean(String key, boolean value) {
getEditor().putBoolean(key, value).commit();
}
public static boolean readBoolean(String key, boolean defValue) {
return getPreferences().getBoolean(key, defValue);
}
// for integer value
public static void writeInteger(String key, int value) {
getEditor().putInt(key, value).commit();
}
public static int readInteger(String key, int defValue) {
return getPreferences().getInt(key, defValue);
}
// for String value
public static void writeString(String key, String value) {
getEditor().putString(key, value).commit();
}
public static String readString(String key, String defValue) {
return getPreferences().getString(key, defValue);
}
// for float value
public static void writeFloat(String key, float value) {
getEditor().putFloat(key, value).commit();
}
public static float readFloat(String key, float defValue) {
return getPreferences().getFloat(key, defValue);
}
// for long value
public static void writeLong(String key, long value) {
getEditor().putLong(key, value).commit();
}
public static long readLong(String key, long defValue) {
return getPreferences().getLong(key, defValue);
}
@SuppressWarnings("static-access")
public static SharedPreferences getPreferences() {
return _context.getSharedPreferences(PREF_NAME, _context.MODE_PRIVATE);
}
public static Editor getEditor() {
return getPreferences().edit();
}
}
那么为什么会这样呢?