Question

Today I encountered a really strange problem in Android. I am using 2 fields with shared preferences. One is deviceId and second is Hashkey. Now first I store only deviceId in sharedprefs, using code

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

then I read hashKey from shared preferences and assign it to a variable in my global application class

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

But problem is when I run this code for first time, the value of hasKey in shared preferences is same as value of deviceID(running on emulator) i.e.

000000000000000

So I first uninstalled the application and then deleted and re created a new emulator, but every time the value of hashkey in shared preferences is same and it is 000000000000000. So how is it possible, because I think, I haven't write the value in shared preferences so it should be null or something else, but definitely not 000000000000000

The output on Log cat is

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

And code of my SharedPref class is

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

So why is it happening ?

Was it helpful?

Solution

In your class SharedPref, you defined constants DEVICE_ID and KEY_HASH as both equal to "". So I guess they refer to the same field in your Shared Preferences. Although it seems weird than an empty field can exists.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top