如何在android中的doinbackground asynctask中添加tabbar

时间:2010-12-10 14:28:27

标签: android

如何在android中的doinbackground asynctask中添加tabbar?或者如何在线程中运行启动画面?我想显示一个进度对话框或启动画面,直到从调用tabbar类加载标签栏我调用webservices并解析值需要时间,几分钟意味着时间,我必须显示progessbar或启动画面。谁能告诉我如何实现这个?任何人都可以提供示例代码吗? 我试过但它不起作用

dlg = ProgressDialog.show(this, "Working..", "Downloading Data...", true, false);    

 Thread splashThread = new Thread() { 
        @Override
        public void run() { 
           try { 

                sleep(100000); 



           } catch (InterruptedException e) { 
              // do nothing 
           } finally { 

           } 
        } 
     }; 
     splashThread.start(); 

1 个答案:

答案 0 :(得分:0)

你不能从后台线程触及任何主ui线程,为了做到这一点,你必须使用管理后台线程和ui主线程之间同步的处理程序。

更好的方法是继承AsyncTask,这样做,你已经有了处理ui的背景和方法的方法。这样,您就可以执行后台操作并立即显示结果......

public class AsynchronousTask extends AsyncTask<Runnable, String, Result> {

//method executed automatically in the ui event before the background thread execution
@Override
    protected void onPreExecute() {
        //show the splash screen and add a progress bar indeterminate
    }

//method executed automatically in the ui event AFTER the background thread execution
@Override
    protected void onPostExecute(Result result) {
        //hide the splash screen and drop the progress bar or set its visibility to gone.
    }

//method executed in a background event when you call explicitly execute...
@Override
    protected Result doInBackground(Runnable... tasks) {
Result result;
        if(tasks != null && tasks.length > 0){
            for (Runnable runnable : tasks) {
                //publishProgress( ... );
                runnable.run();
                                //result = ...
                //publishProgress( ...);
            }
        }
        return result;
    }

}

http://developer.android.com/reference/android/os/AsyncTask.html

在这里阅读更多内容 希望这会有所帮助...