Android Updating Ui Thread Elements - Best Practice?
I have an app that shows some measurement values like temperature, speed and so on. I want to stick more or less to the MVC pattern so I got something that receives the values when
Solution 1:
Have you tried AsyncTask? You can create a class that extends AsyncTask and contains a simple callback interface, something like:
class CalculationTask extends AsyncTask<Integer, Integer> {
...
public interface Callback{
void onCalculationComplete(Integer result);
}
...
}
Now, override doInBackground() method from the AsyncTask and put the program logic for the calculation in it.
@Override
protected int doInBackground(Integer... params){
makeNeededCalculation();
...
}
Once the calculation is complete, the AsyncTask will call its onPostExecute() method. In this method you can refer to your callback interface.
@Override
protected void onPostExecute(Integer result){
mCallback.onCalculationComplete(result);
}
Then you should create an instance of your AsyncTask in the class that receives the values bluetooth and implement the callback interface there.
new CalculationTask(this, new CalculationTask.Callback(){
@Override
public void onCalculationComplete(Integer result){
mView.setText("The new value is "+result);
}
}).execute(valueFromBluetooth);
Post a Comment for "Android Updating Ui Thread Elements - Best Practice?"