Android的 - 我该如何共享preferences从另一个活动?我该、Android、preferences

2023-09-05 06:17:39 作者:三重门。

在我的应用程序有一个按钮(活动1)。当用户点击它,我想在比赛中没有任何声音。我想我应该通过使用共享preferences的相关活动,在按钮的onclick方法做到这一点:

 共享preferences.Editor编辑= M prefs.edit();
editor.putString(声音,1);
editor.commit();
 

声音和游戏开始在另一个活动(活性2)。我需要读取组共享preferences有,但我不知道该怎么做。

感谢

修改

我已经离开这行了:

 共享preferences preFS = preferenceManager.getDefaultShared preferences(Activity1.this);
 
Android第一行代码 SharedPreferences储存

根据您的帮助,在Activity2.class我读了preferences这样

 共享preferences我的preFS = getShared preferences(活动1,MODE_PRIVATE); //Activity1.class
字符串ifsound =我的prefs.getString(声音,);

 如果(ifsound.equals(1))
 {
     Toast.makeText(Activity1.this,1,Toast.LENGTH_LONG).show();
 }
 其他
 {
      Toast.makeText(Activity1.this,0,Toast.LENGTH_LONG).show();
 }
 

在Activity1.class我点击的按钮设置的声音为1。 在我点击另一个BTN打开Activity2.class在那里我总是始终为0,在吐司味精。 解决方案

使用下面的函数添加共享preferences,并从所有活力的影响取保存的值

 公共静态无效使用setdefaults(字符串键,字符串值,上下文语境){
    共享preferences preFS = preferenceManager.getDefaultShared preferences(上下文);
    共享preferences.Editor编辑器= prefs.edit();
    editor.putString(键,值);
    editor.commit();
}

公共静态字符串为getDefaults(字符串键,上下文语境){
    共享preferences preferences = preferenceManager.getDefaultShared preferences(上下文);
    返回preferences.getString(键,NULL);
}
 

In my app there is a button (activity1). When user clicks it i want no sound in the game. I thought i should do this by using sharedpreferences in activity1 in the onClick method of the button:

SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("sound","1");
editor.commit();

The sound and the game starts in another activity (activity2). I need to read the set sharedpreferences there but i don't know how to do it.

Thanks

Edit

I have left this line out:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(Activity1.this);

Based on your help in the Activity2.class I read the preferences like this:

SharedPreferences myPrefs = getSharedPreferences("Activity1", MODE_PRIVATE);  //Activity1.class
String ifsound = myPrefs.getString("sound","");

 if (ifsound.equals("1"))
 {
     Toast.makeText(Activity1.this, "1", Toast.LENGTH_LONG).show();
 }
 else
 {
      Toast.makeText(Activity1.this, "0", Toast.LENGTH_LONG).show();
 }

In Activity1.class i click on the button to set the "sound" to "1". I click on another btn that opens Activity2.class where i always get always "0" in the Toast msg.

解决方案

Use the following functions to add shared preferences and to fetch the saved values from all activiies

public static void setDefaults(String key, String value, Context context) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(key, value);
    editor.commit();
}

public static String getDefaults(String key, Context context) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    return preferences.getString(key, null);
}