I have a server side operation which takes a percentage value from 0 - 100. The user controls this percentage using a SeekBar
on the android client.
Time-wise the operation is expensive, so I don't want to send commands faster than the server can update. To limit the amount of actions sent, I have limited the client it to 10 calls per second by doing the following:
public class ControlPanel extends Activity {
private SeekBar seekbar;
private Thread updateServerThread;
private final Runnable updateServerRunnable = new Runnable() {
@Override
public void run() {
try {
updateServer(seekbar.getProgress());
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.control_panel);
seekbar = (SeekBar) findViewById(R.id.my_seekbar);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekbar, int progress, boolean fromUser) {
if (updateServerThread == null || !updateServerThread.isAlive()) {
updateServerThread = new Thread(updateServerRunnable);
updateServerThread.start();
}
}
@Override public void onStartTrackingTouch(SeekBar seekbar) { }
@Override public void onStopTrackingTouch(SeekBar seekbar) { }
});
}
...