Android的加速度设置:我可以当按钮,它pressed它只能运行。 [英] Android Accelerometer setup: can I get it to run only when button it pressed.
问题描述
我在我的应用程序中使用加速度计,我想知道如果我能做到的方式,加速计仅针对激活后用户一定的时间已经pressed启动按钮。它应该停止时,用户preSS停止按钮。它将基本上工作作为计步器这是一个更大的应用的一部分。这是用来实现加速度计code: -
I am using an Accelerometer in my application and I am wondering if I can make it the way that accelerometer is only activates for a certain amount of time after the user has pressed a start button. It should stops when user press the stop button. It will essentially work as a pedometer which is part of a bigger application. This is the code used to implement the accelerometer:-
SensorManager sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
int sensorType = Sensor.TYPE_ACCELEROMETER;
sensorManager.registerListener(sensorListener, sensorManager.getDefaultSensor(sensorType),SensorManager.SENSOR_DELAY_NORMAL);
我有code计数的步骤是: -
I have the code counting the steps which is:-
final SensorEventListener sensorListener = new SensorEventListener()
{
public void onSensorChanged(SensorEvent event)
{
if(event.values[2]<3)
{
counter ++;
}
else if(event.values[2]>5)
{
counter --;
counter ++;
}
sensorData.setText("Steps = "+ counter);
}
public void onAccuracyChanged(Sensor sensor, int accuracy){}
};
我只是想知道,既然我指望在onSensorChanged方法的步骤我怎么限制这只是一个按钮preSS状态?有没有可能在添加计数器开始计数了多久我前面提到?
I'm just wondering that since I'm counting the steps in the onSensorChanged method how do I limit this to only a button press state? Would it be possible to add a timer in to count how long has passed between the two button presses I mentioned earlier?
推荐答案
是的,当然。
prepare经理:
Prepare manager:
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
启动加速度:
mSensorManager.registerListener(this, mAccelerometer,
SensorManager.SENSOR_DELAY_NORMAL);
停止:
mSensorManager.unregisterListener(this);
不要的onPause()方法。忘记注销监听器
Don't forget unregister listener in onPause() method.
这篇关于Android的加速度设置:我可以当按钮,它pressed它只能运行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!