简体   繁体   English

如何停止ProgressDialog和Thread?

[英]How to stop ProgressDialog and Thread?

i am new to android development. 我是android开发的新手。 i created an sample map application, once we enter the location code, ProgressDialog will be shown while the location is loading. 我创建了一个示例地图应用程序,一旦我们输入了位置代码,就会在位置加载时显示ProgressDialog。 Once the location is displayed, ProgressDialog is to be dismissed, but it keeps on going even after the location is displayed in background. 显示位置后,将关闭ProgressDialog,但即使在背景中显示位置后它仍继续运行。 Below is my code. 下面是我的代码。

Thread background = new Thread(new Runnable() {

                    public void run() {

                        try {
                            Thread.sleep(5000);
                            if(webLocation == true){
                                progressDialog.dismiss();


                            }
                              } catch (InterruptedException e) {

                              e.printStackTrace();

                    }

                }
                });
            background.start();

            }

can anyone give me a solution for stopping the ProgressDialog, once the location is shown. 显示该位置后,谁能给我解决方案以停止ProgressDialog。 i can't get an idea for this solution, if anyone has some ideas or solution, it will be helpful for me. 我对此解决方案一无所知,如果有人有任何想法或解决方案,对我会有所帮助。

Try replacing 尝试更换

 if(webLocation = true) 

by 通过

 if(webLocation == true) or if (webLocation)

You're basically assigning the value true to variable webLocation . 您基本上是将true赋给变量webLocation You should compare, so use == . 您应该比较,所以使用==

You should better use AsyncTask class rather than thread with sleep of 5 seconds (which is hardcoded value). 您最好使用AsyncTask类,而不是使用睡眠时间为5秒(这是硬编码值)的线程。

1) onPreExecute() do the following progressDialog.show() 1) onPreExecute()执行以下progressDialog.show()

2)Get the location in doInBackGround method. 2)在doInBackGround方法中获取位置。 After thar onPostExecute will be called, in this method do the following onPostExecute将被调用之后,在此方法中执行以下操作

   if (progressDialog.isShowing())
       progressDialog.dismiss();

Try this 尝试这个

   Thread background = new Thread(new Runnable() {

                        public void run() {

                            try {
                                Thread.sleep(5000);
    } 
catch (InterruptedException e) {

                                  e.printStackTrace();

                        }
    runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                        .
            // TODO Auto-generated method stub
        enter code here
         if(webLocation = true){
                                    progressDialog.dismiss();


                                }
                            }
                        });



                    }
                    });
                background.start();

                }

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

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