我有一个Android Service
,它实现了SensorEventListener
检测运动。我有一个CheckBoxPreference
,用户可以选择启用或禁用运动检测。我面临的问题是我无法在服务中,即在OnSensorChanged
方法中获得正确的值。
服务在应用程序启动时立即启动。有一个活动启动服务,并与之绑定。
检测运动变化的服务:
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;
}
}
}
}
}
这是我的偏好活动:
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;
}
});
}
所以当我检查和取消检查时,SettingsActivity
的日志显示MotionService.SHAKE_ENABLE
和MotionService.SHAKE_THRESHOLD
的值发生了变化,但在MotionService
的OnSensorChanged
方法中没有反映出来。
我哪里错了?感谢帮助!
为什么要用复选框呢?如果这是你的首选项复选框,那么每个更改都会反映在你的应用程序的首选项中。也就是读取当前偏好状态