Android : Read ChekboxPreference value from Service

82 Views Asked by At

I have an Android Service which implements SensorEventListener for detecting motion. I've a CheckBoxPreference where user can choose to enable or disable motion detection. The problem I'm facing is that I'm not able to get the correct value in the service, namely in the OnSensorChanged method.

The service is started as soon as the application is launched. There is an activity which starts the service, and is binded with it.

Service where I detect the motion change:

    public class MotionService extends Service implements SensorEventListener{

     public static int SHAKE_THRESHOLD = 0;
    public static boolean SHAKE_ENABLE = false;
 @Override
    public void onCreate() {
        if (D) Log.d(TAG, "Creating service");
        super.onCreate();
 SHAKE_THRESHOLD = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getInt("shake_threshold",60)*10;
        SHAKE_ENABLE = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("shake_enable",false);
}

    @Override
        public void onSensorChanged(SensorEvent event) {
            Sensor mySensor = event.sensor;

            if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                float x = event.values[0];
                float y = event.values[1];
                float z = event.values[2];
                long curTime = System.currentTimeMillis();
                if ((curTime - lastUpdate) > 100) {
                    long diffTime = (curTime - lastUpdate);
                    lastUpdate = curTime;

                    float speed = Math.abs(x + y + z - last_x - last_y - last_z)/ diffTime * 10000;

                    Log.d("Sensor Changed",SHAKE_THRESHOLD+"================="+SHAKE_ENABLE); //values here remain the same ones i assign on OnCreate, even though i change it in the settings. It should change when i change from settings. NOT HAPPENING!!

                        if (speed > SHAKE_THRESHOLD) {
                            //do sensor related stuffs here
                            if (SHAKE_ENABLE) { //the boolean value i get from the preferences

                                if ((x) > 8.0000) {
                                    Log.d("sensor", "=====LEFT====");

                                } else if ((x) < -8.0000) {
                                    Log.d("sensor", "=====RIGHT====");


                                } else if ((z) < -0.0) {
                                    Log.d("sensor", "=====UP====");

                                } else if ((y) < 1.0) {
                                    Log.d("sensor", "=====DOWN====");

                                }
                            }
                            last_x = x;
                            last_y = y;
                            last_z = z;
                        }
                }

            }

        }
    }

And this is my preference activity:

public class SettingsActivity extends PreferenceActivity {
@Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Fade it in
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        addPreferencesFromResource(R.xml.settings);
final CheckBoxPreference checkboxPref = (CheckBoxPreference) getPreferenceManager().findPreference("shake_enable");

        checkboxPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                Log.d("MyApp", "Pref " + preference.getKey() + " changed to " + newValue.toString());
                if (newValue.toString().equals("true")){
                    MotionService.SHAKE_ENABLE=true;
                    Log.d("SHAKE_ENABLE",MotionService.SHAKE_ENABLE+"================="+MotionService.SHAKE_THRESHOLD);
                    MotionService.SHAKE_THRESHOLD = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getInt("shake_threshold",60);
                }
                else{
                    MotionService.SHAKE_ENABLE=false;
                    Log.d("SHAKE_ENABLE",MotionService.SHAKE_ENABLE+"================="+MotionService.SHAKE_THRESHOLD);
                    MotionService.SHAKE_THRESHOLD = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getInt("shake_threshold",60);
                }
                    return true;
            }
        });
    }

So when I check and unchek, the logs from the SettingsActivity show that values of MotionService.SHAKE_ENABLE and MotionService.SHAKE_THRESHOLD are changed, but it is not reflected in the OnSensorChanged method of the MotionService.

Where am I going wrong? Help Appreciated!

1

There are 1 best solutions below

1
On

Why you even bother checkbox? if this is your preference checkbox then each change is reflected in your app's preferences. So basically just read current state of your preference