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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:45:35+00:00 2026-05-31T16:45:35+00:00

Within one Activity I have th following piece of code: public void onStartMonitoringToggleClicked(View v)

  • 0

Within one Activity I have th following piece of code:

   public void onStartMonitoringToggleClicked(View v) {
    // Perform action on clicks
    if (((ToggleButton) v).isChecked()) {
        monitoringCanRun = true;
        new Thread(new Runnable() {
        public void run() {
            performMonitoring(); //start the monitor
        }
        }).start();

    } else {
        monitoringCanRun = false;
    }
    }

    public void performMonitoring() {
    while (monitoringCanRun) {
        float parametervalue = Monitor.getParameterValue();

        //need to send the parameter value to the parameter screen's handler now
        Message msg = mHandler
            .obtainMessage(MESSAGE_UPDATE_PARAMETER_VALUE);
        Bundle bundle = new Bundle();
        bundle.putFloat(PARAMETER_VALUE, parametervalue);
        msg.setData(bundle);
        mHandler.sendMessage(msg);
        Log.i("x", "sending MSG------>"); //this gets called EVERY 4 seconds
        try { //we delay to make debugging easier
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        }

    }


    public final int MESSAGE_UPDATE_PARAMETER_VALUE = 1;
    public final String PARAMETER_VALUE = "paramVal";

    //the handler that receives the parameter values from the monitoring thread
    public final Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        Log.i("x", "<-------RECEIVED MSG");  //this gets called only ONCE???????

        switch (msg.what) {
        case MESSAGE_UPDATE_PARAMETER_VALUE: {
        float parameter_value = msg.getData().getFloat(PARAMETER_VALUE);
        if (DEBUG)
            Log.i(this.getClass().getSimpleName(),
                "-> "
                    + Thread.currentThread().getStackTrace()[2]
                        .getMethodName()
                    + " parameterValue="
                    + String.format("%4.2", parameter_value));

        }
        break;

        }

    }
    };

I have used handlers quite alot, but never had this kind of problem. Is there anything basic wrong here?

Many thanks

  • 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-31T16:45:36+00:00Added an answer on May 31, 2026 at 4:45 pm

    Have you tried not using the thread? Could you try to use the handler where it calls its self every 4 seconds? I have modified a project where I use Handler’s to do call backs. Yes I would be on the UI thread but it doesn’t look like you are doing any blocking IO. I may be mistaken.

    public void onStartMonitoringToggleClicked(View v) {
      // Perform action on clicks
      if (((ToggleButton) v).isChecked()) {
        monitoringCanRun = true;
        mHandler.postDelayed(mUpdateTimeTask, 4000);
    
      } else {
        monitoringCanRun = false;
        mHandler.removeCallbacks(mUpdateTimeTask);
      }
    }
    
    private Runnable mUpdateTimeTask = new Runnable(){
      public void run(){
        mHandler.removeCallbacks(mUpdateTimeTask);
        if(monitoringCanRun){
          mHandler.postDelayed(mUpdateTimeTask, 1000);
          float parametervalue = Monitor.getParameterValue();
    
          Log.i(this.getClass().getSimpleName(),
                "-> "
                    + Thread.currentThread().getStackTrace()[2]
                        .getMethodName()
                    + " parameterValue="
                    + String.format("%4.2", parameter_value));
        }
      }
    };
    

    Also, why not just save the data into a global share and access it across threads. I think the way you are doing it you should have access to the threads data. Again this is just an idea. I hope this helps!

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

Sidebar

Related Questions

I have two views within one .xib (one view for landscape, another for portrait).
My situation is following. I have one Activity that contains three main parts. At
I have a async task called within an activity: public class DownloadFile extends AsyncTask<String,
i have two activities within an ActivityGroup, one ProvinceActivity and another CityActivity. so code:
Possible stupid question: let's say I have two apps contained within one project. Can
I have an Android activity running with a custom view in it. When something
I'm trying to implement multiple spinners within one activity, which appears to be working
I am planning on having 3 fragmentlists contained within one activity. The goal is
I have two classes, one is a standard activity , the other a service
I have an activity with a couple of views in a linear layout, one

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.