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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:25:55+00:00 2026-06-07T08:25:55+00:00

My Android application uses a thread to listen for a sockets connection from a

  • 0

My Android application uses a thread to listen for a sockets connection from a PC connected via USB. At some point after the PC opens the connection (in response to some user driven event) I want to send some data over it.

public void onCreate(Bundle savedInstanceState) {

    // SNIP: stuff and nonsense

    connection = new Thread(new ServerThread());
    connection.start();
}

public boolean onTouchEvent(MotionEvent event) {

    // SNIP: decide what to do; create string 'coordString'

    Message coordMsg = coordHandler.obtainMessage();
    Bundle coordMsgData = new Bundle();
    coordMsgData.putString("coords", coordString);
    coordMsg.setData(coordMsgData);
    if(coordHandler!=null)
    {
        coordHandler.sendMessage(coordMsg);
    }

    return false;
}

public class ServerThread extends Thread
{   
    public void run() {

        this.setName("serverThread");

        Looper.prepare();

        coordHandler = new Handler()
        {
            @Override
            public void handleMessage(Message msg) {
                Log.v(INNER_TAG,"here");
            }
        };

        // SNIP: Connection logic here

        Looper.loop();
    }
}.

For ages I was scratching my head wondering why I never saw the value of INNER_TAG appear in my logs after touch events. I could use log-debugging to trace the execution into the coordHandler!=null block, but the handler never seemed to fire.

Then it struck me: the thread is probably exiting after completing the connection. D’uh! Not quite sure what I thought was happening before, but I’ll blame it on a belief that Loop was doing something magical.

So my question is: How can I keep my thread running? The official Android dev reference on threads briefly mentions that

A Thread can also be made a daemon, which makes it run in the background.

This naturally made my *nix senses tingle. (Incidentally, have you seen the new Spiderman film yet? It’s pretty good.) Is daemonisation the answer? Or have I completely lost the plot?

  • 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-07T08:25:57+00:00Added an answer on June 7, 2026 at 8:25 am

    Use Service in Android.

    You can use Service to run at the background..

    See this link:

    http://developer.android.com/reference/android/app/Service.html

    See this link for the example:

    Example: Communication between Activity and Service using Messaging

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

Sidebar

Related Questions

I'm developing an android application which uses web service to get data from server,
I'm currently developing an android application that uses sockets to connect to a local
I have this android application which uses java-javascript bridge to pick up information from
I'm creating a Android application which uses a Switch . I'm trying to listen
My Android application uses Java OAuth library, found here for authorization on Twitter. I
My Android application uses a secret key to generate a token for authentication purposes.
My open-source Android application uses this in an SQL query: String.format(%f, someDoubleValue); Unfortunately, in
I have an Android application that uses the Android database to create tables and
I am building an android application that uses several third party libraries. I have
I am writing an Android application which uses several 3D models. Such a model

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.