简体   繁体   中英

How to stop ProgressDialog and Thread?

i am new to android development. i created an sample map application, once we enter the location code, ProgressDialog will be shown while the location is loading. Once the location is displayed, ProgressDialog is to be dismissed, but it keeps on going even after the location is displayed in background. 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. 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 . You should compare, so use == .

You should better use AsyncTask class rather than thread with sleep of 5 seconds (which is hardcoded value).

1) onPreExecute() do the following progressDialog.show()

2)Get the location in doInBackGround method. After thar onPostExecute will be called, in this method do the following

   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();

                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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