Java 尝试在SharedReferences上使用edit.getBoolen时出错
根据我在这里得到的解决方案,我尝试制作一个切换按钮,以发出每日通知。 它不太管用。。 首先,我得到了“无法解析方法'getBoolean(java.lang.String,boolean)'” 然后我尝试将“编辑器”更改为SharedReferences,这很好,但仍然: 开关按钮的状态并没有保持应有的状态(当我打开开关并退出应用程序时,它会返回到关闭状态),基本上,即使开关关闭,通知仍然会出现。。 我该怎么办? 我正在使用片段 **编辑:LukeJanyga与quicklernear和ahmad一起帮助了我!!但现在通知不来了** 下面是Settings.java:Java 尝试在SharedReferences上使用edit.getBoolen时出错,java,android,sharedpreferences,Java,Android,Sharedpreferences,根据我在这里得到的解决方案,我尝试制作一个切换按钮,以发出每日通知。 它不太管用。。 首先,我得到了“无法解析方法'getBoolean(java.lang.String,boolean)'” 然后我尝试将“编辑器”更改为SharedReferences,这很好,但仍然: 开关按钮的状态并没有保持应有的状态(当我打开开关并退出应用程序时,它会返回到关闭状态),基本上,即使开关关闭,通知仍然会出现。。 我该怎么办? 我正在使用片段 **编辑:LukeJanyga与quicklernear和ahma
public class Settings extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_settings, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().setTitle("הגדרות");
final Switch dailySwitch = (Switch) getView().findViewById(R.id.dailyTipSwitch);
final SharedPreferences sharedPreferences = getActivity().getSharedPreferences("key", 0);
dailySwitch.setChecked(sharedPreferences.getBoolean("key", false));
final SharedPreferences.Editor editor = sharedPreferences.edit();
editor.commit();
dailySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
boolean showNotifications = sharedPreferences.getBoolean("key",false);
if (dailySwitch.isChecked() != showNotifications) {
editor.putBoolean("key",!showNotifications).commit();
}
else {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,20);
calendar.set(Calendar.MINUTE,50);
Intent intent = new Intent(getActivity().getApplicationContext(),NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity().getApplicationContext(),100,intent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(alarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
}
}
});
}
}
以下是NotificationReceiver.java:
public class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Intent repeatingIntent = new Intent(context,MainActivity.class);
repeatingIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
NotificationCompat.Builder mBuilder = new
NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle().bigText("בוקר טוב אלוף! תאכל ותשתה טוב!"))
.setContentText(context.getString(R.string.NotificationText))
.setContentTitle(context.getString(R.string.GoodMorningNotification))
.setAutoCancel(true);
notificationManager.notify(100, mBuilder.build());
}
}
我认为问题在于提交更改时编辑器对象的问题 //将更改保存在SharedReferences中
editor.commit()代码>//提交更改
使用此选项。而不是:
boolean showNotifications = editor.getBoolean("key",false);
使用SharedReference
对象获取值:
boolean showNotifications = sharedPreferences.getBoolean("key",false);
还可以使用editor.commit()代码>而不是editor.apply()代码>正在重新创建片段,因此需要根据首选项中存储的内容相应地设置开关
您不时使用不同的键-“键”与“开关值”
此行将始终将其设置为false
editor.putBoolean("switchValue", dailySwitch.isChecked());
从SharedReferences
而不是编辑器中读取值
:
sharedPreferences.getBoolean("key",false);
并记住在重新创建视图时保留所需的状态。加:
dailySwitch.setChecked(sharedPreferences.getBoolean("switchValue", false));
需要在编辑器中提交更改:
editor.commit();
或
编辑:好的,关于你在评论中提到的,这里有一个非常酷的要点:
不要忘记将您的接收者添加到AndroidManifest.xml
非常感谢!现在正在保存开关状态!但是通知没有出来it@MatanCohen更新了答案清单上有个打字错误!非常感谢你!你是个救生员!!!!!!修复了输入错误,但仍然没有发送通知:(跟随Git。我所做的一切看起来都很好,可能有解决方案吗?您使用的API级别是什么?
editor.apply();