简体   繁体   English

线程和progressDialog android

[英]Thread and progressDialog android

I have a function that requires a lot of calculation, so I try to call that function inside a thread. 我有一个需要大量计算的函数,因此我尝试在线程内调用该函数。 This is how I do it: 这是我的方法:

  submit.setOnClickListener(new OnClickListener() {

        public void onClick(View view) {
            if(!(fromAdress.getText().toString().length() > 0) || !(toAdress.getText().toString().length() > 0)) {
                Toast.makeText(this, "ERROR IN INPUT", Toast.LENGTH_LONG).show();       
            }
            else {
                if(connected) {

                    final ProgressDialog dialog = ProgressDialog.show(this, "Calculating", 
                            "Please wait...", true);
                            final Handler handler = new Handler() {
                               public void handleMessage(Message msg) {
                                  dialog.dismiss();
                                  }
                               };
                            Thread checkUpdate = new Thread() {  
                               public void run() {

                                  measureDistanceAndTime(fromAdress.getText().toString(), toAdress.getText().toString());

                                  handler.sendEmptyMessage(0);
                                  }
                               };
                            checkUpdate.start();
                }

                else
                    Toast.makeText(this, "No network!", Toast.LENGTH_LONG).show(); 
            }

When I open the application, it shows the progressdialog, but after like 4 seconds, it stops responding. 当我打开应用程序时,它显示进度对话框,但是大约4秒钟后,它停止响应。 I cant figure out this problem, cuz when I remove the code for the thread and progressdialog, it works fine! 我无法弄清楚这个问题,因为当我删除线程和progressdialog的代码时,它正常工作!

Isn't this the way to do it? 这不是这样做的方法吗?

I'm not exactly sure why your particular situation isn't working, but you'll probably have better luck using an AsyncTask instead of trying to do raw threads. 我不太确定为什么您的特定情况不起作用,但是使用AsyncTask而不是尝试做原始线程可能会更好。 It handles most of the UI vs. other thread strangeness that can lead to problems. 它处理大多数UI与其他可能导致问题的线程异常。 The conceptual model isn't much different than what you've got there, so it should be an easy change. 概念模型与您所拥有的模型没有太大不同,因此应该轻松进行更改。

The easiest way to do this is with an AsyncTask . 最简单的方法是使用AsyncTask Read this article entitled Painless Threading . 阅读标题为“ 无痛线程处理”的文章。

您需要一个AsynTask

Because you are doing in UI Thread. 因为您正在使用UI Thread。 So when some message action hold over 5 seconds, they will notice infamous message: not responsing. 因此,当某些消息操作持续5秒钟以上时,他们会注意到臭名昭著的消息:不响应。

You should put long-work in other thread. 您应该在其他线程上花费大量时间。 In your case, because you are interacting with UI THread, the most benefit way is use Asyntask 在您的情况下,因为您正在与UI THread交互,所以最有利的方法是使用Asyntask

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM