Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8424941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:08:31+00:00 2026-06-10T04:08:31+00:00

public class MainActivity extends Activity implements Runnable{ private int progressBarStatus = 0; private Handler

  • 0
public class MainActivity extends Activity implements Runnable{

    private int progressBarStatus = 0;
    private Handler progressBarHandler = new Handler();
    ProgressBar linProgressBar;
    private long fileSize = 0;
    Thread t1;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button b1 = (Button)findViewById(R.id.button1);
       Button b2 = (Button)findViewById(R.id.button2);

        b2.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View arg0) 
            {

                linProgressBar.setProgress(0);
                t1.interrupt();
            }

         });

    b1.setOnClickListener(new OnClickListener()
     {

        @Override
        public void onClick(View arg0) 
        {

            basicInitializations();

        }

     });

    }


    public void basicInitializations(){

        linProgressBar = (ProgressBar) findViewById(R.id.progressBar1);
        linProgressBar.setProgress(0);
        linProgressBar.setMax(100);

        try{
            t1 = new Thread() 
            {
                public void run() {
                    while (progressBarStatus < 100) {

                        // process some tasks
                        progressBarStatus = doSomeTasks();

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

                        // Update the progress bar
                        progressBarHandler.post(new Runnable() {
                            public void run() {
                                linProgressBar.setProgress(progressBarStatus);
                            }
                        });
                    }

                    if (progressBarStatus >= 100) {

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

                    }
                }
            };

            t1.start();

        }catch (Exception e) {

        }


    }
    public int doSomeTasks() {

        while (fileSize <= 1000000) {

            fileSize++;

            if (fileSize == 100000) {
                return 10;
            } else if (fileSize == 200000) {
                return 20;
            } else if (fileSize == 300000) {
                return 30;
            }else if (fileSize == 400000) {
                return 40;
            }else if (fileSize == 500000) {
                return 50;
            }

        }

        return 100;

    }


    @Override
    public void run() {
        // TODO Auto-generated method stub

    }
}

this is my main code and i could not stop this thread .
i want to stop it when i click the button b2(say cancel button).
the above code is not the original code and is a model , so please tell me how to stop that thread .

thankyou in advance . . . .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:08:33+00:00Added an answer on June 10, 2026 at 4:08 am

    I have a alternate answer can try the same, inside your runnable loop always check a flag as run_flag, create it as a member variable and set it as true once you stared the loop, you can make it run_flag as false wenever you are done and at the same time you can set null to your thread also … a safer way to come out through runnable loop and thread.

    try{
            t1 = new Thread() 
            {
                public void run() {
                    while (progressBarStatus < 100 && run_flag == true) { // added run_flag here
    
                        // process some tasks
                        progressBarStatus = doSomeTasks();
    
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
    
                        // Update the progress bar
                        progressBarHandler.post(new Runnable() {
                            public void run() {
                                linProgressBar.setProgress(progressBarStatus);
                            }
                        });
                    }
    
                    if (progressBarStatus >= 100) {
    
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) 
                        {
                            e.printStackTrace();
                        }
    
                    }
                }
            };
    
            t1.start();
    
        }catch (Exception e) {
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class MainActivity extends Activity implements SensorEventListener { private TextView temperaturelabel; private SensorManager sensormanager;
public class MainActivity extends Activity implements OnClickListener, AdapterView.OnItemSelectedListener, OnCheckedChangeListener{ @Override public void onCreate(Bundle icicle)
I have following code: public class readSensorsData extends Activity implements SensorListener { /** Called
I have this structure: 1) main activity: public class mainActivity extends Activity { @Override
I have the following class: public class MainActivity extends Activity { /** Called when
I have 2 tabs, named : Tab1, Tab2 class MainActivity extends TabActivity. MainActivity.java public
public class tryAnimActivity extends Activity { /** * The thread to process splash screen
public class A { private A(int param1, String param2) {} public static A createFromCursor(Cursor
I got some invalid Token parsing problem Code : public class viewparty extends Activity
Have following setup: MainActivity class - extends activity MyLayout class - extends View Prefs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.