我正在開發一個android應用程序,我必須同時聽取接近傳感器和加速計傳感器。棘手的部分是我必須在相同的服務中使用它。我可以在同一服務中聆聽兩個傳感器嗎?
我知道我必須在onCreate()
方法中單獨註冊每個聽衆。但我只能有一個onSensorChanged()
方法。對?
是否有可能區分onSensorChanged()
方法中的聽衆?
還有沒有其他方法可以做到這一點?或者可以首先完成?
在此先感謝。
我正在開發一個android應用程序,我必須同時聽取接近傳感器和加速計傳感器。棘手的部分是我必須在相同的服務中使用它。我可以在同一服務中聆聽兩個傳感器嗎?
我知道我必須在onCreate()
方法中單獨註冊每個聽衆。但我只能有一個onSensorChanged()
方法。對?
是否有可能區分onSensorChanged()
方法中的聽衆?
還有沒有其他方法可以做到這一點?或者可以首先完成?
在此先感謝。
Ofcourse:
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
// Accelerometer Sensor
} else if (event.sensor.getType() == Sensor.TYPE_GRAVITY) {
// Gravity Sensor
}
}
是,從文檔中,你將看到onSensorChanged
方法獲取傳遞參數:
public final void onSensorChanged(SensorEvent event)
而且SensorEvent
有Sensor
場命名的傳感器,你可以用它來區分哪種傳感器具有一個新的值