gpt4 book ai didi

安卓 : SharedPreferences in BroadcastReceiver RadioButton's integer value do not update?

转载 作者:太空宇宙 更新时间:2023-11-03 11:12:43 25 4
gpt4 key购买 nike

我有更改单选按钮的 Activity 。

在oncreate方法中

     sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
radioGroup = (RadioGroup)findViewById(R.id.radiogroup);
radioGroup.setOnCheckedChangeListener(radioGroupOnCheckedChangeListener);

实现覆盖的方法并获取单选按钮保存共享偏好

 RadioGroup.OnCheckedChangeListener radioGroupOnCheckedChangeListener =
new RadioGroup.OnCheckedChangeListener(){

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {

RadioButton checkedRadioButton = (RadioButton)radioGroup.findViewById(checkedId);
int checkedIndex = radioGroup.indexOfChild(checkedRadioButton);

savePreferences("remaindertype_toggle_value",checkedIndex);
Log.e("Chenge", String.valueOf(checkedIndex)); // here the get proper value of checkdIndex
}};

实现 sharePreference 方法...

private void savePreferences(String key, int data) {

SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(key, data);
editor.commit();
Log.e("Chengeinside", String.valueOf(value));// Here also get the proper value of the data..
}

现在当接收AlarmReceiver扩展BroadcastReceiver

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub

SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context);

TypeToggleValue = sharedPreferences.getInt(RemainderType_Toggle, 0);

Log.e("AppToggleValue", String.valueOf(TypeToggleValue));// here when first time run the application get the proper value but change the value and secound time get the value its does not updated

}

我也在维护 AndroidMainifest.xml 文件

  <receiver 
android:name="AlarmReceiver"
android:process=":remote"
/>

问题出在 onReceive.. 第一次“checkedIndex”字段是正确的。第二次,如果 checkedIndex 在 radioButton 更改时更新,但是,它返回第一个值。该值似乎没有得到更新...

最佳答案

这里的AndroidMainifest.xml文件中改变了receiver的值...

 <receiver 
android:name="AlarmReceiver"
/>

关于安卓 : SharedPreferences in BroadcastReceiver RadioButton's integer value do not update?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22347442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com