无法根据存储在&中的值在UI上勾选复选框取自SharedPreferences

时间:2014-07-11 04:31:08

标签: android sharedpreferences

我正在使用LightSensor和Checkbox来启用或禁用用户的LightSensor。使用SharedPreferences来存储和检索CheckBox状态,但无法使其按照我想要的方式工作:

  1. 如果用户点击LightSensor复选框并启动操作并退出应用程序,当他返回该应用程序时,LightSensor复选框应该在UI上“勾选” - 即使我'也不会发生这种情况使用了onResume()方法并实现了我的代码,如下所示:

  2. 如果用户单击LightSensor复选框并启动操作并退出应用程序,当他返回该应用程序时,在这种情况下,在UI上取消选中LightSensor Checkbox。这很好。

  3. onPause(),onResume()下面的代码片段。没有错误,但它没有像我上面解释的那样工作。:

    最新代码如下所示

    @Override
            public void onPause() {
                super.onPause();
                SharedPreferences.Editor editor = mSharedPreferences.edit();
                editor.putBoolean("LightSensorValue", isLightSensorON);
                editor.commit();
                // on pause turn off the flash
                turnOffFlash();
               // tggle_Text_Button.setChecked(false);
                mSensorManager.unregisterListener(this);
            }
    
    
            @Override
            public void onResume() {
                super.onResume();
    
                // on resume turn on the flash
                if (hasFlash) {
    
                    // turnONFlash();
                    tggle_Text_Button.setChecked(false);
                    // Setting the Default Value for the CheckBox to 'False' during
                    // resume and retrieving the actual value
                    // from the SharedPreferences
                    if (mSharedPreferences.getBoolean("LightSensorValue", false)) {
                        ChkBox_LightSensor.setChecked(true); // to Tick The CheckBox
                                                                // on the UI if the
                                                                // SharedPreferences
                        // returns 'True' value
                        isLightSensorON = true;
                        turnONLightSensor();
                    } else {
                        ChkBox_LightSensor.setChecked(false);
                        isLightSensorON = false;
                        turnOFFLightSensor();
                    }
                }
    
            }
    
            @Override
            public void onStart() {
                super.onStart();
    
                // on starting the app get the camera params
                getCamera();
            }
    
            @Override
            public void onStop() {
                super.onStop();
    
                // on stop release the camera
                if (camera != null) {
                    camera.release();
                    camera = null;
                }
            }
    
            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
                // TODO Auto-generated method stub
    
            }
    
            /*
             * 
             * When light sensor detects, it's value below <2, the flash light
             * automatically gets ON and in case if the light sensor detects, it's
             * values above 2...the flash light automatically gets OFF
             */
            @Override
            public void onSensorChanged(SensorEvent event) {
                // TODO Auto-generated method stub
    
                float lux = event.values[0];
                int luxInt = (int) lux;
                // tV_light_Value.setText(String.valueOf(lux));
                Log.d("BRK0018 - LightSensor", "New Light value occurred : " + lux
                        + "   :   " + luxInt);
                if (event.values[0] <= 2.0) {
                    turnONFlash();
                    Log.d("BRK0018 - LightSensor",
                            "New Light value LOW occurred : " + luxInt);
                    Log.d("BRK0018 - LightSensor",
                            "LightSensor switched on the Flash");
                } else {
                    turnOffFlash();
                    Log.d("BRK0018 - LightSensor", "New Light value is HIGH : "
                            + luxInt);
                    Log.d("BRK0018 - LightSensor",
                            "LightSensor switched OFF the Flash");
                }
    
            }
    
            // When Light Sensor Checkbox is checked, below is the initialization of
            // the LightSensor and the
            // SensorManager
    
            @Override
            public void onClick(View view) {
                // TODO Auto-generated method stub
                boolean isLightSensorON = ChkBox_LightSensor.isChecked();
                if (isLightSensorON) {
                    turnONLightSensor();
                } else {
                    turnOFFLightSensor();
                }
    
            }
    
            private void turnONLightSensor() {
                // TODO Auto-generated method stub
                SharedPreferences.Editor editor = mSharedPreferences.edit();
                editor.putBoolean("LightSensorValue", isLightSensorON);
                editor.commit();
                isLightSensorON = true;
                mSensorManager = (SensorManager) getActivity().getSystemService(
                        Context.SENSOR_SERVICE);
                if (mSensorManager.getSensorList(Sensor.TYPE_LIGHT).size() != 0) {
                    Sensor mLightSensor = mSensorManager.getSensorList(
                            Sensor.TYPE_LIGHT).get(0);
                    mSensorManager.registerListener(this, mLightSensor,
                            SensorManager.SENSOR_DELAY_NORMAL);
                    Toast.makeText(getActivity(), "LightSensor is ACTIVE now", 1000)
                            .show();
                }
            }
    
            private void turnOFFLightSensor() {
                // TODO Auto-generated method stub
                isLightSensorON = false;
                SharedPreferences.Editor editor = mSharedPreferences.edit();
                editor.putBoolean("LightSensorValue", isLightSensorON);
                editor.commit();
    
                Toast.makeText(getActivity(), "Light Sensor is INACTIVE now", 1000)
                        .show();
            }
    
        }
    

2 个答案:

答案 0 :(得分:2)

从您的代码中我不知道您是否曾从用户输入中设置isLightSensorON = true;。同样,每个onPause您都会将偏好设置为偏好设置,因此在每个onResume您都会将其设置为假。

答案 1 :(得分:0)

**我最新更新的答案。**

分享细节,以便对像我这样的其他新手有所帮助。

我已尝试mSensorManager.unregisterListener(this);else置于onResume()以及'turnOFFLightSensor()',但在这两种情况下,该应用都崩溃而没有任何错误注释。

然后,再次查看代码,发现了错误:(

onClick()的{​​{1}}中,我已将LightSensor Checkbox放置在onPause()之外。这解决了这个问题。

** Bottomline,简单来说**当我取消选中该框时,立即取消注册LightSensor。这让我的一天! :)

mSensorManager.unregisterListener(this);

BTW, 非常感谢@Eugen Martynov 一直帮助我!