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 6203937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:00:09+00:00 2026-05-24T05:00:09+00:00

So in my further attempts to implement a while loop in android I have

  • 0

So in my further attempts to implement a while loop in android I have come up with the code below :

private boolean connected = false;
private Thread loop = new Thread() {
    public void run() {
        Looper.prepare();
        while (connected) {
            do_Something();
        }
        Looper.loop();
    }
};

onCreate() {
    //.....
    ok = (Button) findViewById(R.id.button1);
    ok.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (connected) {
                try {
                    loop.start();
                }
                catch (Exception e) {
                    Toast.makeText(getBaseContext(), "Exception caught", Toast.LENGTH_LONG).show();
                }
            }
            return true;
        }
    });
    stop = (Button)findViewById(R.id.button2);
    stop.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                //disconnects current connection
                connected = false;
            }
            return true;
        }
    });
}

That is, I am trying to on the touch of the first button have my UI thread switch to the thread that will do_something over and over again until the touch of the second button, in which case the boolean var k will switch off and completely stop the newly created thread from the button press of the first button. I have google’d “threads/handlers/android while loops” but to no avail. Any help towards what I am trying to do would be much appreciated

Simply put, how do I kill the thread that was created via pressing the second button?

  • 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-05-24T05:00:11+00:00Added an answer on May 24, 2026 at 5:00 am

    Did you tried AsyncTask ? What you can do…start a new AsyncTask on firstButton click and cancel it on secondButton click.

    //define global variable
    private DoSomething doSomething;
    
    //register firstButton onClickListener
    firstButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            //start your asynctask
            if(doSomething == null || doSomething.isCancelled()){
                doSomething = new DoSomething();
                doSomething = doSomething.execute();
            }
        }
    });
    
    //register secondButton onClickListener
    secondButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            doSomething.cancel();
        }
    });
    
    //Inner AsyncTask class
    class DoSomething extends AsyncTask<Void, Void, Void>{
        @Override
        protected Void doInBackground(Void... params) {
            //doSomething();
            while(true){
                System.out.println(1);
                if(isCancelled()){
                    break;
                }
            }
            return null;
        }
    }
    

    Note: This is pseudocode…might contain error…just want to give you overview. hope this help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have searched through Google, SO and Android developers but I need some further
I have some code which must be thread safe whose behavior resembles this: protected
I have included my source code draft below. I would appreciate any input on
This is a further question based on this answer: How can I implement a
I have two classes SccmAction and TicketAction which both implement interface IDelivery. These classes
If I have a program which creates and attempts to open a named pipe
I'm trying to compare two arrays in groovy. My attempts so far have yielded
Further to my question on disabling validators using javascript - Disable ASP.NET validators with
Further to How to Pass Command Line Parameters in batch file how does one
Further to this question: Algorithm for determining a file’s identity Recap : I'm looking

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.