开发者

Android refuses to write/create sharedpreferences file

开发者 https://www.devze.com 2023-02-28 02:00 出处:网络
I am using the Eclipse 3.5.2 IDE for Android development.I am using the following code to write to the default sharedpreferences file and I am running it in the adb emulator:

I am using the Eclipse 3.5.2 IDE for Android development. I am using the following code to write to the default sharedpreferences file and I am running it in the adb emulator:

private SharedPreferences getOurSharedPreferences(Activity act) {
    // return getSharedPreferences(SHARED_PREFS_FILENAME, MODE_PRIVATE);
    return act.getPreferences(MODE_PRIVATE);
}

private void putStringToStorage(Activity act, String keyName, String s) {

    // Store it.
    SharedPreferences sharedPrefs = getOurSharedPreferences(act);
    SharedPreferences.Editor editor = sharedPrefs.edit();
    editor.remove(keyName);
    // editor.putString(keyName, s);
    editor.putString(keyName, "test123");
    boolean bCommitted = editor.commit();
    if (!bCommitted) 
        throw new RuntimeException("(AndroidApplication) Unable to save new string.");

    // Get it back as a test.
    // String s2 = getStringFromStorage(keyName);
}

The above method belongs to an Application sub-class of mine, that is why the method carries an Activity object parameter so I can have access to Activity.getPreferences(). Note, previously I was using the global getSharedPreferences() call but I changed to getPreferences() out of desperation due to the problems I am having. The reason I am writing "test123" instead of the variable "s" is to make sure the problem I'm having is not related to some value of "s" that perhaps the shared preferences file might not like.

Whenever I run the code editor.commit() always comes back FALSE. Some history. A while back I was able to write to the shared preferences file using a shared preferences file named "app_global", created by using the global getSharedPreferences() call instead of Activity.getPreferences(). I was able to write and read value from that shared preferences file, however, whenever I re-launched my app the values I wrote during the last session were gone. As part of my debugging efforts I went into the adb shell and deleted "app_global" from my app's shared_prefs folder. Ever since then the emulator appears unwilling to create a new shared preferences file, default one or otherwise.

Note, the LogCat window is always empty and the Console window does not show any errors, including during the launch of my application in the emulator. I also tried launching the application in Debug mode with "Wipe User Data" checked in the Debug configuration. That did nothing to help either. No matter what I do editor.commit() fails and the shared_prefs folder for my application when doing an "ls -a" from the adb shell is always empty.

My questions are:

  • I read in passing somewhere about "rebuilding the user image" for the emulator. Is that a technique I could use to try t开发者_JS百科o fix this cluster of problems I am having with shared preference storage? If so, how do I do that?

  • Is there anything else I could try to fix this problem?

-- roschler


I managed to solve the problem by manually recreating the sharedpreferences file. I can't be certain of this, but it appears that if you manually delete a sharedpreferences file belonging to the emulator, it will not recreate it even if you choose the "Wipe User Data" option. To manually recreate the sharedpreferences file I did the following:

  • Load the ADB shell
  • Change to the sharedpreferences folder (note you must have the emulator running or you will get a "device not found" error, assuming you don't have a phone connected either)
  • Create an empty file with the name of the sharedpreferences file you use in your code. In my case I created a file called "app_global".

You can find the sharedpreferences folder here:

/data/data/your.app.package.name/shared_prefs

Where your.app.package.name is your application's package name. To create the empty file from within the ADB shell I used the command:

echo -n >app_global

Where "app_global" is whatever you named your sharedpreferences file. The -n option prevents a line feed from being thrown into the file. If you are using getPreferences() then the name of the default sharedpreferences file is "your.app.package.name_preferences.xml".

I got the information about the sharedpreferences folder from this StackOverflow post:

How to examine SharedPreferences from adb shell?

-- roschler

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号