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:
classCalculationTaskextendsAsyncTask<Integer, Integer> {
...
publicinterfaceCallback{
voidonCalculationComplete(Integer result);
}
...
}
Now, override doInBackground()
method from the AsyncTask
and put the program logic for the calculation in it.
@Override
protectedintdoInBackground(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.
@OverrideprotectedvoidonPostExecute(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.
newCalculationTask(this, newCalculationTask.Callback(){
@OverridepublicvoidonCalculationComplete(Integer result){
mView.setText("The new value is "+result);
}
}).execute(valueFromBluetooth);
Post a Comment for "Android Updating Ui Thread Elements - Best Practice?"