本文介绍了与Handler类帮助更新的用户界面 - 安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望你能帮助我:

我需要更新我的用户界面Android应用程序,我想使用handler类来做到这一点,使用的和Android开发人员资源共同任务使用的处理程序指南。

I need to update my ui for an android app and I'm trying to use the Handler class to do it, using http://developer.android.com/resources/articles/timed-ui-updates.html and the android developer resources "Common Task" for using Handlers as guides.

基本上,我需要在两者之间的东西 - 用户界面的一个定时更新,但没有一个按钮。因此,这里是相应和code是我的工作。所有的帮助是极大的AP preciated。

Basically, I need something between the two - a timed update of the user interface, but without a button. So here is the relevent code that I am working on. All help is greatly appreciated.

public class Activity1 extends Activity {

[...变量]

[… variables]

最后的处理程序mHandler =新的处理程序();

final Handler mHandler = new Handler();

final Runnable mUpdateResults = new Runnable() {
    public void run() {

        UpdateDisplay();
        mHandler.postDelayed(mUpdateResults, 200);
    }
};


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

[...]

[…]

startLongRunningOperation();

startLongRunningOperation();

}


protected void startLongRunningOperation() {

    Thread t = new Thread() {
        public void run() {
             if (mStartTime == 0L) {
                 mStartTime = System.currentTimeMillis();
                 mHandler.postDelayed(mUpdateResults, 200);}
            mHandler.post(mUpdateResults);
        }
    };
    t.start();
}

再次感谢!

推荐答案

在Android上最好用的在后台执行任务,而(逐步)更新的用户界面从这项任务的结果。

On Android it's best to use AsyncTask to execute tasks in the background while (progressively) updating UI with results from this task.

编辑:

检查code后,我觉得你的处理程序正常工作。也许问题出在 UpdateDisplay()。既然你要更新从后台线程的显示,确保你调用 [view.postInvalidate()] [2] 大功告成更新查看后。

After checking code I think your Handler works correctly. Probably problem is in UpdateDisplay(). Since you are updating display from background thread, make sure you call [view.postInvalidate()][2] after you're done updating your View.

这篇关于与Handler类帮助更新的用户界面 - 安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 07:38