[英]How to register or unregister a sensor event listener on click of a button?
I am using the following code to register and unregister SensorEventListener. 我正在使用以下代码注册和注销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. 在活动的onPause()
方法中编写相同的代码时,我没有收到此错误。 What is the problem and how to correct this? 有什么问题,如何解决?
this
refers to the OnClickListener
instead of your Activity
. this
是指OnClickListener
而不是Activity
。
Change it to this: 更改为此:
smanager.registerListener(YourActivityClass.this, smanager.getDefaultSensor.TYPE_LINEAR_ACCELERATION, SensorManager.SENSOR_DELAY_NORMAL);
Edit to answer your context
comment: 编辑以回答您的context
评论:
registerListener()
requires a SensorListener
. registerListener()
需要一个SensorListener
。 According to your 根据你的
it works in onResume() 它在onResume()中工作
comment, I assumed that your Activity
implements the SensorListener
interface. 注释,我假设您的Activity
实现了SensorListener
接口。 The context itself does not implement it therefor you get the same error. 上下文本身并未实现它,因此您将得到相同的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.