I am using the following code to register and unregister SensorEventListener.
//Get the Toggle Button
final ToggleButton tb=(ToggleButton) findViewById(R.id.activate);
//Listener for ToggleButton
tb.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if(tb.isChecked()){
//Register the sensor
//smanager.
smanager.registerListener(this, smanager.getDefaultSensor.TYPE_LINEAR_ACCELERATION,SensorManager.SENSOR_DELAY_NORMAL);
Log.v(classname, "Sensor Listener Unregistered");
}
else{
//deRegister the Sensor
// Unregister the listener
smanager.unregisterListener(this);
Log.v(classname, "Sensor Listener Unregistered");
}
}
});
But I am getting the following error.
The method registerListener(SensorListener, Sensor, int) is not applicable for the arguments new View.onClickListener(),{},Sensor,int
I am not getting this error when writing the same code in onPause() method of the activity. What is the problem and how to correct this?
thisrefers to theOnClickListenerinstead of yourActivity.Change it to this:
Edit to answer your
contextcomment:registerListener()requires aSensorListener. According to yourcomment, I assumed that your
Activityimplements theSensorListenerinterface. The context itself does not implement it therefor you get the same error.