所以我试图在10秒后启动sensor reading method
并在说5分钟后停止它。这是相同的代码。
case R.id.btn_pos_poll_side: {
final Handler handler = new Handler();
//Delay Runner Here
handler.postDelayed(new Runnable() {
@Override
public void run() {
side_output.setText("Delayed Data Collection");
//Start reading LIGHT SENSOR Data, Handle Null Light Sensor Data
if (LightSensor != null || mProximity != null || mAccelerometer != null || mGyroscope != null) {
//listen to light sensor.
mySensorManager.registerListener(
MultiSensorListener,
LightSensor,
SensorManager.SENSOR_DELAY_NORMAL);
//listen to proximity sensor.
mySensorManager.registerListener(
MultiSensorListener,
mProximity,
SensorManager.SENSOR_DELAY_NORMAL);
//listen accelerometer, note this has 3-axes.
mySensorManager.registerListener(
MultiSensorListener,
mAccelerometer,
SensorManager.SENSOR_DELAY_NORMAL);
//listen gyroscope, note this has 3-axes.
mySensorManager.registerListener(
MultiSensorListener,
mGyroscope,
SensorManager.SENSOR_DELAY_NORMAL);
//print output text
side_output.setText(Html.fromHtml("<h3> ----- Data Collection Session Starts Here -----</h3>"));
} else {
side_output.setText("No Sensor Found!");
}
}
}, 10000);
final Handler closeHandler = new Handler();
//Data Sender Runner Here
closeHandler.postDelayed(new Runnable() {
@Override
public void run() {
Button stopCollection = (Button) findViewById(R.id.btn_stop_poll_side);
stopCollection.performClick();
}
}, 120000);
break;
}
然而,这永远不会奏效,甚至收集的数据也有些损坏(新数据与之前的数据相连)。
我做错了什么?
感谢。
修改:更多细节。我想在按下按钮10秒后启动它,然后在说2分钟后停止它。除非我再次按下按钮,否则只会发生一次。
已添加:停止按钮逻辑
case R.id.btn_stop_poll_side: {
// remove sensor listener
mySensorManager.unregisterListener(MultiSensorListener,
LightSensor);
mySensorManager.unregisterListener(MultiSensorListener,
mProximity);
mySensorManager.unregisterListener(MultiSensorListener,
mAccelerometer);
mySensorManager.unregisterListener(MultiSensorListener,
mGyroscope);
side_output.append("\n" + sensorReading);
/*
Reading data and writing to Dropbox!
*/
new DropboxTask(side_output, "Back Pocket Data Reading", sensorReading).execute();
break;
}
答案 0 :(得分:1)
当用户第一次按下按钮时:
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
// Do what you need to do
}
}
handler.postDelayed(runnable, 10_000 /* Wait 10 seconds */);
当用户第二次按下按钮时:
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
// Stop what you want to stop
}
}
handler.postDelayed(runnable, 2*60*1000 /* Wait 2 minutes */);
这必须在按钮的onClick
中完成:
首先定义一个名为isButtonAlreadyClicked = false
的私有布尔值。
现在:
yourButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if(!isButtonAlreadyClicked) {
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
// Do what you need to do
}
}
handler.postDelayed(runnable, 10_000 /* Wait 10 seconds */);
isButtonAlreadyClicked = true;
} else {
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
// Stop what you want to stop
}
}
handler.postDelayed(runnable, 2*60*1000 /* Wait 2 minutes */);
isButtonAlreadyClicked = false;
}
});
修改强>
如果你想阻止runnable在等待时运行,只需确保在你的方法之外(在你的类中)声明处理程序和你的runnables,这样你就可以(例如):
mHandler.removeCallbacks(firstRunnable);
mHandler.removeCallbacks(secondRunnable);
通过这种方式,它不再运行Runnables了。