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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:43:34+00:00 2026-05-20T20:43:34+00:00

Following Googles examples for using a Service, I created a couple of threads like

  • 0

Following Googles examples for using a Service, I created a couple of threads like this. I can’t use IntentService, because I’m doing something that involves waiting for callbacks.

However, I don’t know how to terminate a Thread started in this manner. As I understand it, Threads automatically terminate when the run() method returns. However, this kind of thread doesn’t have a run method. My Threads are leaking–they stay alive after stopSelf().

@Override
public void onCreate() {

    HandlerThread thread = new HandlerThread("ServiceStartArguments",
            android.os.Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();
    HandlerThread thread2 = new HandlerThread("CallbackHandling",
            android.os.Process.THREAD_PRIORITY_BACKGROUND);
    thread2.start();

    mServiceLooper = thread.getLooper();
    mCallbackLooper = thread2.getLooper();
    mServiceHandler = new MyHandler(mServiceLooper);
    mCallbackHandler = new Handler(mCallbackLooper);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();

    // For each start request, send a message to start a job and deliver the
    // start ID so we know which request we're stopping when we finish the job
    Message msg = mServiceHandler.obtainMessage();
    msg.arg1 = startId;
    mServiceHandler.sendMessage(msg);
    mMainThreadHandler=new Handler();
    // If we get killed, after returning from here, restart
    return START_STICKY;
}

private final class MyHandler extends Handler {
    public MyHandler(Looper looper) {
        super(looper);
    }
    @Override
    public void handleMessage(Message msg) {
        cycle();


        // Stop the service using the startId, so that we don't stop
        // the service in the middle of handling another job
        stopSelf(msg.arg1);
    }
}

protected void cycle() {
    ...
    mCallbackHandler.post(new Runnable(){
        public void run(){
            goAskForSomeCallbacks();
        }
    });

    try {
        Thread.sleep(GIVE_UP_TIME);
    } catch (InterruptedException e){
        //The callback will interrupt this thread to stop it from waiting
        Log.d(TAG,"Got early callback, stop waiting.");
    }
    Thread.interrupted(); //clear the interrupt
    doStuff(); 

}
  • 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-20T20:43:35+00:00Added an answer on May 20, 2026 at 8:43 pm

    Try calling the quit method on the corresponding Loopers.

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

Sidebar

Related Questions

I am using XMPP example from this address. I am putting the following configuration
I am trying to install Googles' eclipse plugin - http://code.google.com/eclipse/docs/getting_started.html Even on following all
Edit: closing anchor fixed. This issue exists when testing on the following browsers: Google
By using the following tool oAuth Playground , and following the documentation Google Documents
I need to get the domain name from an URL. The following examples should
This seems like a simple question, but I haven't been able to find the
There are tons of great examples of creating work items using the TFS API,
When logging out or sending (to a web service using RestKit) specifically the subThoroughfare
I have this use case that is very similar to the robot-legs example of
Ii'm trying to share a Google Spreadsheet using C#. Because the Google Data 3.0

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.