2013-08-16 31 views
0

我正在開發一個android應用程序,我必須同時聽取接近傳感器和加速計傳感器。棘手的部分是我必須在相同的服務中使用它。我可以在同一服務中聆聽兩個傳感器嗎?

我知道我必須在onCreate()方法中單獨註冊每個聽衆。但我只能有一個onSensorChanged()方法。對?

是否有可能區分onSensorChanged()方法中的聽衆?

還有沒有其他方法可以做到這一點?或者可以首先完成?

在此先感謝。

回答

1

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 
    } 
} 
0

是,從文檔中,你將看到onSensorChanged方法獲取傳遞參數:

public final void onSensorChanged(SensorEvent event) 

而且SensorEventSensor場命名的傳感器,你可以用它來區分哪種傳感器具有一個新的值

相關問題