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

The Archive Base Latest Questions

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

I am writing a flashlight app that is a screen light for any phone

  • 0

I am writing a flashlight app that is a “screen light” for any phone without a torch. I want to have a setting that enables a Strobe Light feature. However, with this type of “flashlight” I thought the easiest way to do this, would be to change the background of the view. I do this in a separate thread because I use a SeekBar to get how fast the strobelight should flash. However, I keep getting the error that the Thread cannot access the View because it is not the Thread that created it. Any suggestions?

Here is what is contained within the onCreate():

        mSeekBar = (SeekBar) findViewById(R.id.seekBar);
        mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
          public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {

                final Thread strober = new Thread(new Runnable(){
                    public void run(){
                        strobeLight();
                    }
                });             
                if (mSeekBar.getProgress()>0) {
                    runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        strober.start();
                    }
                    });
                }
                else {
                    runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        strober.interrupt();
                        relLayout.setBackgroundColor(color.all_white);
                    }
                    });
                }
          }
          public void onStartTrackingTouch(SeekBar seekBar) {}
          public void onStopTrackingTouch(SeekBar seekBar) {}          
        }); 

Here is the strobeLight() method:

public void strobeLight(){
    boolean lightIsOn=true;

    runOnUiThread(new Runnable() {
    @Override
    public void run() { 
            do {
                    if (lightIsOn){
                        relLayout.setBackgroundColor(color.all_black);
                        try { Thread.sleep(1000/(mSeekBar.getProgress()+2)); }
                            catch (InterruptedException e) {}
                        lightIsOn=false; 
                        strobeActivated=true;}
                    else{
                        relLayout.setBackgroundColor(color.all_white);  
                        try { Thread.sleep(1000/(mSeekBar.getProgress()+2)); }
                            catch (InterruptedException e) {}                       
                        lightIsOn=true; }
                    if (mSeekBar.getProgress()<1)
                        strobeActivated=false;
            } while (strobeActivated);
            relLayout.setBackgroundColor(color.all_white);
    }
});   
}   

Here is the LogCat for the code above:

11-28 17:17:58.383: E/ActivityManager(16877): ANR in com.marshall.meadows182 (com.marshall.meadows182/.LightNoTorch),  time=603431510
11-28 17:17:58.383: E/ActivityManager(16877): Reason: keyDispatchingTimedOut
11-28 17:17:58.383: E/ActivityManager(16877): Load: 1.53 / 1.62 / 1.62
11-28 17:17:58.383: E/ActivityManager(16877): CPU usage from 14662ms to 0ms ago:
11-28 17:17:58.383: E/ActivityManager(16877):   0.8% 16877/system_server: 0.4% user + 0.4% kernel / faults: 7 minor
11-28 17:17:58.383: E/ActivityManager(16877):   0% 14673/com.qikffc.android: 0% user + 0% kernel / faults: 151 minor
11-28 17:17:58.383: E/ActivityManager(16877):   0.2% 16957/com.android.systemui: 0.2% user + 0% kernel / faults: 1 minor
11-28 17:17:58.383: E/ActivityManager(16877):   0.1% 16999/com.android.phone: 0% user + 0% kernel
11-28 17:17:58.383: E/ActivityManager(16877):   0.1% 13/kondemand/0: 0% user + 0.1% kernel
11-28 17:17:58.383: E/ActivityManager(16877):   0% 68/rild: 0% user + 0% kernel / faults: 6 minor
11-28 17:17:58.383: E/ActivityManager(16877):   0% 14830/logcat: 0% user + 0% kernel
11-28 17:17:58.383: E/ActivityManager(16877):   0.1% 14868/com.marshall.meadows182: 0% user + 0% kernel / faults: 9 minor
11-28 17:17:58.383: E/ActivityManager(16877):   0% 24825/adbd: 0% user + 0% kernel
11-28 17:17:58.383: E/ActivityManager(16877):   0% 5446/com.google.android.apps.maps:NetworkLocationService: 0% user + 0% kernel
11-28 17:17:58.383: E/ActivityManager(16877):   0% 7839/wpa_supplicant: 0% user + 0% kernel / faults: 3 minor
11-28 17:17:58.383: E/ActivityManager(16877):   0% 17054/com.google.process.gapps: 0% user + 0% kernel / faults: 4 minor
11-28 17:17:58.383: E/ActivityManager(16877):   0% 25055/iqd: 0% user + 0% kernel
11-28 17:17:58.383: E/ActivityManager(16877): 2.7% TOTAL: 1.8% user + 0.8% kernel
11-28 17:17:58.383: E/ActivityManager(16877): CPU usage from 416ms to 935ms later with 99% awake:
11-28 17:17:58.383: E/ActivityManager(16877):   7.4% 16877/system_server: 0% user + 7.4% kernel / faults: 6 minor
11-28 17:17:58.383: E/ActivityManager(16877):     9.2% 16923/InputDispatcher: 1.8% user + 7.4% kernel
11-28 17:17:58.383: E/ActivityManager(16877): 3.8% TOTAL: 0% user + 3.8% kernel
  • 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:47:36+00:00Added an answer on May 27, 2026 at 4:47 am

    The error message is self-explanatory. You are not allowed to call many methods of views outside of the Thread they were created on, known as the UI Thread.

    The problem is the lines

    relLayout.setBackgroundColor(...);
    

    which are run in your thread. You should run them on the UI thread like this:

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            relLayout.setBackgroundColor(...);
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Writing a PHP app and have several classes that only have static methods (no
Writing a test app to emulate PIO lines, I have a very simple Python/Tk
Writing an app that will include the ability to decompress zip and rar files.
Writing an iPhone app in which I want to save the user the grief
Writing an iPhone app in Objective-C, I have a date in string form (in
Writing Scala code, I regularly encounter cases where I have processor functions that operate
Writing a small app that ( among other things ) lets users upload a
Writing GWT application. I have CellTable and a code that i'v got from google-example
Writing a stored procedure that will have multiple input parameters. The parameters may not
Writing some test scripts in IronPython, I want to verify whether a window is

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.