0

今天我在Android中遇到了一個非常奇怪的問題。 我正在使用共享首選項的2個字段。一個是deviceId,另一個是Hashkey。 現在開始我只存儲在sharedprefs DEVICEID,使用代碼什麼是共享首選項中字段的默認值?

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的在共享偏好的值是相同的設備ID(在仿真器上運行),即

000000000000000 

所以我第一UNINST的值然後刪除並重新創建了一個新的模擬器,但每次共享首選項中的hashkey值都是000000000000000.那麼怎麼可能,因爲我認爲我沒有在共享首選項中寫入值所以它應該是零或別的東西,但絕對不是0000000億

上登錄貓的輸出是

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(); 
} 
} 

那麼,爲什麼會發生?

回答

1

在你的類SharedPref中,你定義的常量DEVICE_ID和KEY_HASH都等於「」。 所以我想他們是指共享首選項中的相同字段。雖然它看起來很奇怪,但可能存在一個空白字段。