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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:17:26+00:00 2026-06-08T21:17:26+00:00

Can some please suggest an alternative to using two different handlers in the one

  • 0

Can some please suggest an alternative to using two different handlers in the one activity, or let me know that this method is OK?

Details:
I’m in the process of developing some code for use with Microchips ADK Android starter kit. Everything is running smoothly, however, I have an issue where there are two different handlers running in the code, it works but I have noticed that it is not recommended.
The first handler I use to create a time delay, and use the post command to launch the result. The second handler is used in Microchip’s sample code that they supplied to communicate with their USB accessory framework files. I prefer to only tinker with the Microchip code rather than rewriting it.
Similar code:

public class MainActivity extends Activity 
{   ...
    final Handler mHandler = new Handler(); // handler for startTimeDelay
    ...
    protected void onCreate(Bundle savedInstanceState)
    {   super.onCreate(savedInstanceState);
        ...
        Button btnSetTimeDelay = (Button) findViewById(R.id.btnChangeTimeDelay);
        btnSetTimeDelay.setOnClickListener
            (   new View.OnClickListener() 
                {   public void onClick(View v) 
                    {
                        setBackgroundColorLinearLayout(color.holo_red_dark); //red background
                        displayLockIsOpen(false);   // display closed lock message
                        startTimeDelay();   // wait to open the lock
                    }
                }
            );
    } // onCreate
    ...
    final Runnable mUpdateResults = new Runnable()
    {   public void run()
        {   setBackgroundColorLinearLayout(color.holo_green_dark); //green background
            displayLockIsOpen(true);    // display open lock message
        }
    };

    protected void startTimeDelay()
    {   Thread t = new Thread()
        {   @override
            public void run()
            {   SystemClock.sleep(global_delay);
                mHandler.post(mUpdateResults);  // run mUpdateResults code
            };
        };
        t.start();
    } // startTimeDelay

    // USB accessory handler from Microchip, for ADK
    private Handler handler = new Handler()
    {   @override
        public void handleMessage(Message msg)
        {   
            ...
            switch(msg.what)
            {   case USBAccessoryWhat:
                ...lots of code here from Microchip
            } // switch msg.what
        } // handleMessage
    }; // Handler
} // MainActivity 
  • 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-08T21:17:28+00:00Added an answer on June 8, 2026 at 9:17 pm

    You can create a single Handler itself both for startTimeDelay and USB accessory.

    From the code of android.

    public void dispatchMessage(Message msg) {
            if (msg.callback != null) {
                handleCallback(msg);
            } else {
                if (mCallback != null) {
                    if (mCallback.handleMessage(msg)) {
                        return;
                    }
                }
                handleMessage(msg);
            }
        }
    

    Its says when you have a callback(Runnable) it will call handleCallback which will call your Runnable.run Method, else handleMessage will be called.

    So you can have a single handler.

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

Sidebar

Related Questions

Can some one please explain why this loop never 'breaks' and goes on forever
Possible Duplicate: JavaScript: formatting number with exactly two decimals Can some one please help
Possible Duplicate: Operator overloading Hi guys can some one please suggest a good tutorial
Can anyone please suggest some logic behind a an application that syncs notes with
Can some one please suggest what kind of a tablet should I get for
can some buddy please suggest how to disable view button on cgridview widget and
I am newbie in testing. Can you please suggest me some good tutorials of
Can anyone please explain or suggest some good tutorial for the method of matrix
Can some one please tell me why exactly classes were introduced in C++. Are
Can some one please guide me to understand which jar file i need to

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.