Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SharedReferences切换状态?_Java_Android_Sharedpreferences_Togglebutton_Brightness - Fatal编程技术网

Java SharedReferences切换状态?

Java SharedReferences切换状态?,java,android,sharedpreferences,togglebutton,brightness,Java,Android,Sharedpreferences,Togglebutton,Brightness,我有一个开关,可以将设备的亮度从manual更改为authomatic。它可以工作,但按钮的状态不保存。。我现在需要两样东西 1) 使用SharedReferences保存按钮状态 2) 当我打开应用程序时,检查手机的亮度 这是我的onCreate中的切换: autoBrightToggle = (ToggleButton)v.findViewById(R.id.luminosita); autoBrightToggle.setOnClickListener(new View.O

我有一个开关,可以将设备的亮度从
manual
更改为
authomatic
。它可以工作,但按钮的状态不保存。。我现在需要两样东西

1) 使用SharedReferences保存按钮状态 2) 当我打开应用程序时,检查手机的亮度

这是我的onCreate中的切换:

autoBrightToggle = (ToggleButton)v.findViewById(R.id.luminosita);
        autoBrightToggle.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (autoBrightToggle.isChecked()) {
                    setAutoBrightness(true);
                } else {
                    setAutoBrightness(false);
                }
            }
        }); 
方法是:

void setAutoBrightness(boolean value) {
                if (value) {
                    Settings.System.putInt(getActivity().getContentResolver(), SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
                } else {
                    Settings.System.putInt(getActivity().getContentResolver(), SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_MANUAL);
                }
            }
我试过这样做,但不起作用:

sPrefdata  = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); 
        ToggleButton autoBrightToggle = (ToggleButton) findViewById(R.id.brightoggle); //Dichiaro il toggle
        boolean togglebrightness = sPrefdata.getBoolean("DATA", false);  a
            if (togglebrightness ) //if (tgpref) may be enough, not sure
            {

                autoBrightToggle .setChecked(true);
            }
                else
            {
                autoBrightToggle .setChecked(false);
            }
所以在onClick中

SharedPreferences sPref = getSharedPreferences(PREFS_NAME, 0);
            Editor editor = sPref.edit();
            editor.putBoolean("DATA", true); //or false 
            editor.apply();

但是不起作用。不保存状态,方法停止工作。我怎样才能解决这个问题?我如何检查哪一个是实际亮度?

试试下面给出的代码片段,我用它在共享首选项中保存字符串

SharedPreferences.Editor ed = getSharedPreferences("DATA", 0).edit();
ed.putBoolean("DATA", true);
ed.commit();

logcat怎么说?