Android更新ui线程元素-最佳做法? [英] Android updating ui thread elements - best practice?

查看:292
本文介绍了Android更新ui线程元素-最佳做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以显示一些测量值的应用程序,例如温度,速度等.

I have an app that shows some measurement values like temperature, speed and so on.

我想或多或少地坚持MVC模式,所以我得到了一些东西,当它们出现时(从蓝牙组件)接收它们的值,并将它们排序到特殊的值处理程序中.那些应该计算东西,等等(从地理坐标等得到的速度),然后将这些值传递给View组件,该组件扩展了活动并应该打印这些值.一些值处理程序将在它们自己的线程中,或者整个值处理程序将是一个单独的线程.

I want to stick more or less to the MVC pattern so I got something that receives the values when they appear (from a bluetooth component) and sorts them to the special value handlers. those are supposed to calculate stuff and so on (speed from geo coordinates and so on) and pass the values to the View component, which extends activity and is supposed to print the values. Some of the value handlers will be in their own thread or maybe the whole value handler thing will be one single thread.

所以首先我尝试了"runOnUIThread",但这不是我想要的,因为它具有很多值,UI线程中不会发生其他事情,而且也不是MVC模式的想法.

So first I tried "runOnUIThread" but this is not as I want it cause with many values nothing else will happen in the UI thread and it is not in the idea of the MVC pattern.

然后我考虑了处理程序,但遇到一个问题,即我无法从另一个线程找到"处理程序,因此我必须将其传递给我,这是我必须做的很多标头更改.

Then I thought about handlers but I got the problem that I cannot "find" the handler from the other thread so I would have to pass it on and that is a lot of header change I would have to do.

然后,我想到了一个带有静态方法的私有类,该类可以从任何地方访问,但如果聪明的话,则不知道.

Then I thought about a private class with static methods which could be reachable from everywhere but dunno if that is clever.

您有什么建议,可以给我举个例子吗?

What do you suggest and could you give me examples?

推荐答案

您是否尝试过 AsyncTask ?您可以创建一个扩展AsyncTask的类,并包含一个简单的回调接口,例如:

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

现在,从AsyncTask中覆盖doInBackground()方法,并将用于计算的程序逻辑放入其中.

Now, override doInBackground() method from the AsyncTask and put the program logic for the calculation in it.

@Override
protected int doInBackground(Integer... params){
    makeNeededCalculation();
    ...
}

一旦计算完成,AsyncTask将调用其onPostExecute()方法.在这种方法中,您可以引用您的回调接口.

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

然后,您应该在该类中创建一个AsyncTask实例,该实例接收蓝牙值并在那里实现回调接口.

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

这篇关于Android更新ui线程元素-最佳做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆