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

  • Home
  • SEARCH
  • 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 8260961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:14:20+00:00 2026-06-08T03:14:20+00:00

I have this ToggleButton that is enabled when a certain condition (Website content) is

  • 0

I have this ToggleButton that is enabled when a certain condition (Website content) is true.

getSystemOnState(..) connects to a webserver and this causes an exception because of strict mode. What is wrong about the way I am using the Handler class?

public class ReceiverToggleButton extends ToggleButton {
 private Runnable mTicker;
 private Handler mHandler;
 private boolean mTickerStopped = false;
 private String rxhost = null;
 private Context context = null;

 public ReceiverToggleButton(Context context) {
    super(context);
    this.context = context;
    updateOnOffState(context);
 }

 private void updateOnOffState(final Context cxt) {
    Runnable r = new Runnable() {
        public void run() {
            rxhost = cxt.getResources().getString(R.string.host_receiver);
            mHandler = new Handler();
            mTicker = new Runnable() {
                public void run() {
                    if (mTickerStopped) {
                        return;
                    }

                    boolean isSystemOn = getSystemOnState(rxhost); // connects to webserver
                    setChecked(isSystemOn);
                    invalidate();
                    long now = SystemClock.uptimeMillis();
                    long next = now + 1000 * 10; // check every 10s
                    mHandler.postAtTime(this, next);
                }

            };
            mHandler.post(mTicker);
        }
    };
    new Thread(r).start();
 }
}
  • 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-08T03:14:21+00:00Added an answer on June 8, 2026 at 3:14 am

    Strict mode is complaining because you are still trying to do that network operation on the UI thread. Or this class is getting called into by a BroadcastReceiver (which are short lived).
    Handlers’s are also ment to pass messages and you aren’t really using them correctly in this example. Or at least you can see how all the Threads, runnables, and posting makes everything hard to read.

    What you need here is AsyncTask.

    Here is the Google example From http://developer.android.com/reference/android/os/AsyncTask.html

    private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
         protected Long doInBackground(URL... urls) {
             int count = urls.length;
             long totalSize = 0;
             for (int i = 0; i < count; i++) {
                 totalSize += Downloader.downloadFile(urls[i]);
                 publishProgress((int) ((i / (float) count) * 100));
                 // Escape early if cancel() is called
                 if (isCancelled()) break;
             }
             return totalSize;
         }
    
         protected void onProgressUpdate(Integer... progress) {
             setProgressPercent(progress[0]);
         }
    
         protected void onPostExecute(Long result) {
             showDialog("Downloaded " + result + " bytes");
         }
     }
    
    new DownloadFilesTask().execute(url1, url2, url3);
    

    In your case, you will want the first parameter to be your host string, your do in background will perform your checks and your network call, and the onPostExecute (which runs on the UI thread) to update your views.

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

Sidebar

Related Questions

I have a FrameworkElement (really a ToggleButton ) that has inside its content a
I need to have a command handler for a ToggleButton that can take multiple
I have ToggleButton with an OuterGlowBitmapEffect applied to it. I animate this glow to
Within a Desktop app I have a toggle button: <ToggleButton x:Name=CFStglBtn Checked=cfsCBox_Checked Unchecked=cfsCBox_Unchecked IsChecked=True
I have a ToggleButton that is malfunctioning. As I understand it, a ToggleButton should
I have a four ToggleButton buttons that I want to behave as a group,
So, I have this nice little view that I've made, which basically shows two
I have a GWT 2.0 ToggleButton that I have styled from a ResourceBundle: ToggleButton
I have a ToggleButton inside of a ListBox and when that button is clicked
I have just implemented these ToggleButtons using ControlTemplates that set the Image content according

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.