0
我正在開發Android Wear應用程序,該應用程序從開始註冊心率,直到用戶停止觸摸按鈕爲止,所以我沒有取消在onStop或onDestroy函數上註冊偵聽器。android wear duplicate sensorEventListener
問題是當我返回Wear菜單並再次打開應用程序。在那一刻,應用程序再次註冊聽衆並獲得所有重複的措施。我試圖在onStart和onRestart函數上取消註冊監聽者,但它沒有工作。
這裏是我的代碼:
...
public class MainActivity extends Activity implements SensorEventListener {
Sensor mHeartRateSensor;
SensorManager mSensorManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Sensor and sensor manager
mSensorManager = ((SensorManager)getSystemService(SENSOR_SERVICE));
mHeartRateSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE);
}
@Override
protected void onStart() {
super.onStart();
mSensorManager.registerListener(this, mHeartRateSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onRestart() {
super.onRestart();
mSensorManager.registerListener(this, mHeartRateSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
/*
I need the listeners are active, so i can't unregister it
@Override
protected void onDestroy() {
this.mSensorManager.unregisterListeners(this);
super.onDestroy();
}*/
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
//Get heart rate and accuracy
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
//
}
/*on click listeners*/
...
}
在此先感謝。