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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:27:54+00:00 2026-05-27T04:27:54+00:00

I am trying to write a new feature for a flashlight app. I am

  • 0

I am trying to write a new feature for a flashlight app. I am pretty sure I need a new Thread to do this. HOWEVER, I am new to Threads and do not know how to do it at all. What I am trying to do is on the change of a SeekBar, I want the strobe to get faster/ slower. If it is 0 or 1 resume to constant light.

This is wrong, but this is what I have. Whenever I move the SeekBar it correctly strobes, but you cannot make it stop..

In onCreate():

    mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
        final int myInt = progress;
        new Thread (new Runnable() {
          public void run() {
            if (myInt>1)
                strobeLight(myInt);
            else {}
                // Stop Strobe
          } }).start();
      }
      public void onStartTrackingTouch(SeekBar seekBar) {}
      public void onStopTrackingTouch(SeekBar seekBar) {}          
    });

strobeLight() method:

public void strobeLight(int myInt){
    do {
    if (myInt>1){
            if (strobe){
                processOffClick();
                try { Thread.sleep(300/myInt); }
                    catch (InterruptedException e) {}
                strobe=false; 
                strobeActivated=true;}
            else{
                processOnClick();   
                try { Thread.sleep(300/myInt); }
                    catch (InterruptedException e) {}                       
                strobe=true; }
        }
    else{
        processOnClick();
        strobe=false; 
        Thread.currentThread().interrupt(); 
        strobeActivated=false;}

    } while (strobeActivated);
} 
  • 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-27T04:27:55+00:00Added an answer on May 27, 2026 at 4:27 am

    I am unsure about the need of creating a new thread every time the progress changes, a better approach might be to create a final runnable in which you change the integer value:

    onProgressChange:

        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
            toRun.interval = progress;
        }
    

    A runnable-implementation:

        class StrobeRunnable implements Runnable {
            volatile boolean running;
            volatile int interval = 0;
            private boolean lightIsOn = false;
    
            @Override
            public void run() {
                while(running){
                    if (interval > 1){
                        Thread.sleep(300/interval);
                        lightIsOn = !lightIsOn;
                        switchLight(lightIsOn);
                    }
                    else{
                        Thread.sleep(600/interval);
                        if( !lightIsOn ){
                            lightIsOn = true;
                            switchLight(lightIsOn);
                        }
                    }
                }
            }
        };
    

    Note that I created new names for some methods,
    switchLight(boolean isLightOn) is what I believe your processXClick did before, namely switch the light on or of.
    And interval used to be called myInt.

    To start a thread I prefer to use Executors:

        StrobeRunnable toRun = new StrobeRunnable();
        Executor exec = Executors.newSingleThreadExecutor();
        exec.execute(toRun);
    

    To completely stop the thread from running, set the toRun.running to false, and this will stop the running loop.

    I haven’t tested this but it should work. It also depends a bit on how your processClick-method looks.

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

Sidebar

Related Questions

I'm new to cakephp and trying to write a simple app with it, however
I'm pretty new to XQuery and I'm trying to write an example function that
I am new to php and trying to write a login function. Bit stuck
I'm new to C# and trying to write a simple GUI Unit Test with
I'm new to jQuery and i'm trying to write some code to go through
I am trying to write a decorator to do logging: def logger(myFunc): def new(*args,
I am trying to figure out how to write a new line of text
I am fairly new to c# and am trying to write a n-tiered web
I'm brand new at Python and I'm trying to write an extension to an
Hey I'm new to java servlets and I am trying to write one that

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.