0

我有这个调用 Web 服务并解析 xml 的异步任务

@Override
protected void onPreExecute(){
super.onPreExecute();
time = System.currentTimeMillis();
}
 protected Boolean doInBackground(Integer... params) {
  //code 
 }

protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
    difftime = System.currentTimeMillis() - time;
 }

在执行异步任务时,我想显示一个加载屏幕,但是如果我这样做,加载屏幕会在异步任务完成之前完成

        super.onCreate(savedInstanceState);
        setContentView(R.layout.loading_screen);

            final CallWebService callTarif = new CallWebService(6,sett.getDeviceId());
            callTarif.execute();

new Handler().postDelayed(new Runnable(){ 
        @Override 
            public void run() { 

                LoadingScreen.this.finish(); 
                Intent intent = new Intent(LoadingScreen.this, NextActivity.class);
                                    startActivity(intent);            
            } 

        }
        },callTarif.difftime);
4

3 回答 3

0

实际上postDelayed是在完成AsyncTask之前调用。

只需将这些代码行

LoadingScreen.this.finish(); 
Intent intent = new Intent(LoadingScreen.this, NextActivity.class);
startActivity(intent);     

opPostExecute()AsyncTask 中。

protected void onPostExecute(Boolean result) {
super.onPostExecute(result);

    difftime = System.currentTimeMillis() - time;
    LoadingScreen.this.finish(); 
    Intent intent = new Intent(LoadingScreen.this, NextActivity.class);
    startActivity(intent);      
}

并删除处理程序 new Handler().postDelayed(new Runnable(){

于 2013-01-24T09:26:23.913 回答
0

start your loading screen onPreExecute method and kill it onPostExecute method of the async task

于 2013-01-24T09:27:50.460 回答
0

使用异步任务访问 web 服务时无需使用 Handler 来显示加载。使用onPreExecute()AsyncTask 的方法来显示加载屏幕并在里面完成它,onPostExecute因为该方法在doInBackground执行完成时调用。将代码代码更改为:

      @Override
      protected void onPreExecute() {
            // show loading bar here
      }
@Override
      protected String doInBackground(String... params) {
                 // do network operation here
            return null;
      }      

      @Override
      protected void onPostExecute(String result) {      
          // dismiss loading bar here         
      }
于 2013-01-24T09:29:06.163 回答