Jeet.Deir Jeet.Deir - 3 months ago 6
Android Question

Start And Stop Method Automatically

So I am trying to start my

sensor reading method
after 10 seconds and stop it after say 5 minutes. This is the code for the same.

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


However this never works and even the data collected is somewhat corrupt (new data is concatenated to previous data).

What am I doing wrong?

Thanks.

Edit: Some more details. I want to start it after 10 seconds from when I have pressed the button and stop it after say 2 minutes. This happens only once unless I press the button again.

Added: Stop Button Logic

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

Answer

When user presses the button the first time:

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 */);

When the user presses the button the second time:

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 */);

This has to be done in the onClick of your button:

First define a private boolean like called isButtonAlreadyClicked = false.

Now:

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;
        }
});

EDIT:

If you want to prevent the runnable to be run while it is waiting, just make sure to declare the handler and your runnables outside of your method (inside your class), so that you can do (for example):

mHandler.removeCallbacks(firstRunnable);
mHandler.removeCallbacks(secondRunnable);

In that way it won't run the Runnables anymore.