Arun Kumar
Arun Kumar

Reputation: 877

Problem to stop thread due to progress dialog

I have two buttons: one for start an execution of function as given below with thread and progress dialog

 start_button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Log.d(TAG, "mMainButton clicked");

                    showLoading();
                    // Calling showdialog before downloading..   
                    Thread t = new Thread(){      
                        public void run() {                  
                            startdownload();

                            // Code for download....                 
                            // After download UI thread will call    
                            runOnUiThread(new Runnable() {            

                                public void run() {        
                                    dialog.cancel();     
                                    alertbox("Info","Download Completed");     
                                    }                
                                });             
                            }};           
                            t.start();                                                      
                }
            });

public ProgressDialog dialog;    
        public  void showLoading () {       
            // prepare the dialog box       
            //ProgressDialog            
            dialog = new ProgressDialog(this);           
            // make the progress bar cancelable       
            dialog.setCancelable(true);            
            // set a message text            
            dialog.setMessage("Please wait while Downloading.."); 
            // show it            
            dialog.show();            
            } 
        protected void alertbox(String title, String mymessage)    {   
            new AlertDialog.Builder(this)       
            .setMessage(mymessage)       
            .setTitle(title)      
            .setCancelable(true)       
            .setNeutralButton("OK",          
                    new DialogInterface.OnClickListener() 
            {               
                public void onClick(DialogInterface dialog, int whichButton){}  
            })       .show();  
            } 

    public Boolean startdownload() {    

            try
            {
            downloadhelper = new download_helper(this);

            downloadhelper.DownloadProblemAndReasonCode();      

            pkManifest=downloadhelper.DownloadNewManifest();        

            downloadhelper.DownloadNewMessages();

            //downloadhelper.DeleteOldManifest();


            }
            catch(Exception exception)
            {
                exception.toString();
            }
            return false;
        }

And another button to stop the download as given below:

stop_button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {           
                onStop();
                MessageBox("Down Load Stopped");                

            } 

        });

public void onStop() {
        super.onStop();
        isRunning = false;


            }

The problem is that I am not able to click button to stop the started function due to the progress dialog that comes in front.

Is there any way to stop the execution of the start button function clicked?

Upvotes: 0

Views: 1507

Answers (3)

Samuel
Samuel

Reputation: 9993

you can set the oncancellistner of the progress dialog

ProgressDialog p; 
p.setCancelable(true);
p.setOnCancelListener(new OnCancelListener() {          
@Override
public void onCancel(DialogInterface dialog) {
    //stop thread execution     
}
});

this will cancel the execution if the user cancels the dialog. (back key)

EDIT

in response to Arun's question

i have not tested this

ProgressDialog p; 
p.setButton(ProgressDialog.BUTTON_NEGATIVE, "Cancel", new OnClickListener() {   
@Override
public void onClick(DialogInterface dialog, int which) {    
    //This might work       
}
});

Upvotes: 3

Mohit Verma
Mohit Verma

Reputation: 3025

Use Asynctask to show the progress dialog and to dismiss it. In that show your progress dialog in the OnPreExecute() and dismiss your dialog in OnPostExecute(). See this link: Asynctask Help

Upvotes: 1

Elemental
Elemental

Reputation: 7521

Seems to me the correct solution is to enable the cancel button functionality of the Progress Dialog (which is provided automatically by ProgressDialog) this makes sense for a more android consistent interface too.

Upvotes: 0

Related Questions