Android 异步任务进度条在后台进程完成之前不会出现

发布于 2025-01-03 08:52:00 字数 1641 浏览 0 评论 0原文

我正在构建一个项目,其中使用异步任务来显示进度栏。 我正在使用 get() 方法来等待主线程,以便我们可以在之前执行其他任务。

但在 doInBackground 完成后显示进度条。 我想在加载开始时显示加载栏。 当 onPostExecute 调用时它将关闭。

public class TempConverterActivity extends Activity {   
pojo p;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button b= (Button) findViewById(R.id.btn);
    b.setOnClickListener(new OnClickListener() {     
        @Override
        public void onClick(View v) {
            showResult();    
        }
    });  
}
private void showResult() {         
    try {
        new LoadData().execute().get();                            
    } catch (Exception e) {
        Log.e("async brix--", e.getMessage());
    }  
    runned();
}
private void runned() {
    ArrayList<String> al = p.getData();
    for (String str : al){
        Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();    
    }
}
private class LoadData extends AsyncTask<Void, Void, Void> {
    private final ProgressDialog dialog = new ProgressDialog(TempConverterActivity.this);
    protected void onPreExecute() {
       dialog.setMessage("Loading data...");
       dialog.setIndeterminate(true);
       dialog.setCancelable(false);
       dialog.show();
    }
    protected void onPostExecute(final Void unused) {  
       if (dialog.isShowing()) {
          dialog.dismiss();      
       }           
    }
    @Override
    protected Void doInBackground(Void... params) {
        p = new pojo();
        new SoapParser(p);
        return null;
    }
 }}

请帮忙。提前致谢。

I am building a project in which i use async task to show progress bar.
I am using get() method to wait the main thread so we can do the other task before .

but progress bar is showing after completion of doInBackground thered.
I Want to show the loading bar when the loading starts.
It will dismiss when onPostExecute calls.

public class TempConverterActivity extends Activity {   
pojo p;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button b= (Button) findViewById(R.id.btn);
    b.setOnClickListener(new OnClickListener() {     
        @Override
        public void onClick(View v) {
            showResult();    
        }
    });  
}
private void showResult() {         
    try {
        new LoadData().execute().get();                            
    } catch (Exception e) {
        Log.e("async brix--", e.getMessage());
    }  
    runned();
}
private void runned() {
    ArrayList<String> al = p.getData();
    for (String str : al){
        Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();    
    }
}
private class LoadData extends AsyncTask<Void, Void, Void> {
    private final ProgressDialog dialog = new ProgressDialog(TempConverterActivity.this);
    protected void onPreExecute() {
       dialog.setMessage("Loading data...");
       dialog.setIndeterminate(true);
       dialog.setCancelable(false);
       dialog.show();
    }
    protected void onPostExecute(final Void unused) {  
       if (dialog.isShowing()) {
          dialog.dismiss();      
       }           
    }
    @Override
    protected Void doInBackground(Void... params) {
        p = new pojo();
        new SoapParser(p);
        return null;
    }
 }}

Please help . Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

攒眉千度 2025-01-10 08:52:01

编辑:在我之前的回答中,我建议使用处理程序;然而,AsyncTask 消除了我没有发现的这样做的需要。

为什么您觉得需要调用 AsyncTask.get()?这是一个阻塞调用,并且您从 UI 线程中调用它,因此最终是先运行它还是先运行 onPreExecute() 的竞争条件。

我认为没有理由在这种情况下调用 get() 。您希望在 AsyncTask 完成后调用 runned(),但您可以通过从 onPostExecute() 启动新线程来实现此目的。或者,您可以像现在一样使用 get(),但从新线程而不是 UI 线程调用它。

Edited: In my previous answer I suggested using a Handler; however, AsyncTask eliminates the need to do this which I didn't spot.

Why do you feel the need to call AsyncTask.get()? This is a blocking call, and you call this from the UI thread, thus it is ultimately a race condition as to whether it or onPreExecute() is run first.

I see no reason why you should call get() in this context. You want to call runned() after the AsyncTask completes, but you could do this by launching a new thread from onPostExecute(). Alternatively you could do as you do now, using get(), but call that from a new thread instead of the UI thread.

倥絔 2025-01-10 08:52:00

您可以尝试以下代码,

progDailog = ProgressDialog.show(loginAct,"Process ", "please wait....",true,true);

new Thread ( new Runnable()
{
     public void run()
     {
      // your code goes here
     }
}).start();

 Handler progressHandler = new Handler() 
 {

     public void handleMessage(Message msg1) 
     {
         progDailog.dismiss();
     }
 }

You can try following code,

progDailog = ProgressDialog.show(loginAct,"Process ", "please wait....",true,true);

new Thread ( new Runnable()
{
     public void run()
     {
      // your code goes here
     }
}).start();

 Handler progressHandler = new Handler() 
 {

     public void handleMessage(Message msg1) 
     {
         progDailog.dismiss();
     }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文