尝试在SharedPreferences上使用edit.getBoolen时出错

时间:2017-04-27 17:32:35

标签: java android sharedpreferences

根据解决方案,我已经到了这里,我尝试制作一个切换按钮进行每日通知。

它运作良好.. 首先我得到"无法解决方法' getBoolean(java.lang.String,boolean)'" 比我试图更改"编辑器"到SharedPreferences,它很好,但仍然:

开关按钮的状态不应该保持原样(当我打开开关并退出应用程序时它会重新关闭)并且基本上即使显示开关,通知仍然会出现.. 我该怎么办? 我使用片段

**编辑:LukeJanyga帮助我和quicklernear和ahmad一起!但现在通知没有来**

这是Settings.java:

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());
    }
}

3 个答案:

答案 0 :(得分:0)

我认为问题在于提交更改时编辑器对象

//保存SharedPreferences中的更改

editor.commit(); //提交更改

使用此功能。

答案 1 :(得分:0)

而不是:

 boolean showNotifications = editor.getBoolean("key",false);

使用sharedPreference对象获取值:

 boolean showNotifications = sharedPreferences.getBoolean("key",false);

也使用editor.commit();代替editor.apply();

答案 2 :(得分:0)

您的片段正在重新创建,因此您需要根据首选项中存储的内容设置Switch

您不时使用不同的密钥 - “key”vs“switchValue”

此行始终将其设为false

editor.putBoolean("switchValue", dailySwitch.isChecked());

SharedPreferences而不是Editor

读取值
sharedPreferences.getBoolean("key",false);

请记住在重新创建视图时保留所需的状态。添加:

dailySwitch.setChecked(sharedPreferences.getBoolean("switchValue", false));

需要在编辑器中提交更改:

editor.commit();

editor.apply();

编辑:好的,关于你在评论中提到的内容 - 这里有一个非常酷的要点: https://gist.github.com/BrandonSmith/6679223

不要忘记将您的接收器添加到AndroidManifest.xml