SharedPreferences值不坚持

时间:2013-04-12 10:42:19

标签: c# android xamarin.android

我正在尝试将字符串保存到SharedPerferences,如下所示:

var prefs = Application.Context.GetSharedPreferences ("Janssen", FileCreationMode.WorldReadable);  

var prefEditor = prefs.Edit ();
prefEditor.PutString ("SecurityToken", SecurityCode.Text);
prefEditor.Commit ();

读取

var prefs = Application.Context.GetSharedPreferences ("Janssen", FileCreationMode.WorldReadable);  

var SecurityToken = prefs.GetString ("SecurityToken", null);

我是来自iOS的Android新手。我想在iOS中模仿NSUserDefaults。我在新活动中阅读SharedPreferences,也许是导致问题的原因?我不确定。我也不知道将什么设置为默认值。

2 个答案:

答案 0 :(得分:3)

您是否在编写SharedPreferences和阅读它们之间重新部署?这通常会擦除SharedPreferences

我刚刚对它们进行了测试,它们可以在两个Activities之间正常工作:

WritePrefsAcitivity.cs

using Android.App;
using Android.Content;
using Android.Widget;
using Android.OS;

namespace Derrrp
{
    [Activity (Label = "Derrrp", MainLauncher = true)]
    public class WritePrefsActivity : Activity
    {
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            SetContentView (Resource.Layout.Write);

            var prefs = Application.Context.GetSharedPreferences("MySharedPrefs", FileCreationMode.Private);
            var prefsEditor = prefs.Edit();

            var ed = FindViewById<EditText>(Resource.Id.editText1);
            ed.AfterTextChanged += (sender, e) => {
                prefsEditor.PutString("MyPref", e.Editable.ToString());
                prefsEditor.Commit();
            };

            Button button = FindViewById<Button> (Resource.Id.myButton);

            button.Click += delegate {
                var intent = new Intent(this, typeof(ReadPrefsActivity));
                StartActivity(intent);
            };
        }
    }
}

ReadPrefsActivity.cs

using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget;

namespace Derrrp
{
    [Activity (Label = "ReadPrefsActivity")]            
    public class ReadPrefsActivity : Activity
    {
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            SetContentView(Resource.Layout.Read);

            var tv = FindViewById<TextView>(Resource.Id.myTextView);
            var button = FindViewById<Button>(Resource.Id.myButton);

            var prefs = Application.Context.GetSharedPreferences("MySharedPrefs", FileCreationMode.Private);

            button.Click += (sender, e) => {
                tv.Text = prefs.GetString("MyPref", "");
            };
        }
    }
}

defValue可以设置为您想要的任何内容。如果您尝试获取的首选项为空,则会选择此值。

注意:如果您使用它来存储一些秘密信息,建议使用FileCreationMode.Private,甚至使用使其仅可被应用程序读取的内容,例如加密。< / p>

GIF image showing the two activities working.

答案 1 :(得分:0)

我不确定您的错误是什么,但下面是我之前用于使用共享首选项保存字符串的示例。 我曾经将默认值设置为“”,这样在读取时,如果之前未保存该值,则会将变量设置为空字符串。您可以在任何新活动中阅读共享首选项,共享首选项是每个应用程序而不是活动..

示例:

写字符串:

 String pass = "abcdefg";
 SharedPreferences preferences = getSharedPreferences("pref", 0);
 SharedPreferences.Editor editor = preferences.edit();
 editor.putString("savedPass", pass);
 editor.commit();

阅读字符串:

 SharedPreferences preferences = getSharedPreferences("pref", 0);
 String mypassword = preferences.getString("savedPass", "");