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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:07:32+00:00 2026-05-28T16:07:32+00:00

My MainActivity starts another thread called UdpListener. In onResume, the thread will be started

  • 0

My MainActivity starts another thread called UdpListener. In onResume, the thread will be started (this works), in onPause it should stop, but socket.close() causes an error message in LogCat “the system call was canceled“. The UdpListener itself works fine in JUnit tests so this must be some kind of Android problem.

These are the corresponding methods of my thread:

private void setThreadRunning(boolean running) throws MyOwnException {
    this.running = running;

    if (running) {
        try {
            serverSocket = new DatagramSocket(9800);
        } 
        catch (SocketException e) {
            exceptionThrown = true;
            this.running = false;
            Log.e(TAG, e.getMessage());

            if (serverSocket != null && !serverSocket.isClosed())  {
                serverSocket.close();
            }

            throw new MyOwnException(e);
        }
    } 
    else {
        if (serverSocket != null && !serverSocket.isClosed())  {
            serverSocket.close();
        }
    }
}

public void stopThread() throws MyOwnException {
    setThreadRunning(false);
}

MainActivity.onPause():

@Override
public void onPause()  {
    super.onPause();

    try {
        udpListener.stopThread();
    } 
    catch (MyOwnException e) {
        //TODO AlertDialog
    }
}

serverSocket.close() of else branch will be executed and causes the LogCat entry, but why?

UPDATE
Maybe Dalvik recognize this as error because close() throws SocketExceptions on every thread currently block in “receive”, but I’m not sure if this Exception is related to this problem.

http://docs.oracle.com/javase/1.4.2/docs/api/java/net/DatagramSocket.html#close()

  • 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-28T16:07:33+00:00Added an answer on May 28, 2026 at 4:07 pm

    When I surround the receive() call in run method with a SocketException try..catch, the error doesn’t occure anymore. I guess that my exception handling had some side effect to cause this error.

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

Sidebar

Related Questions

I have a Activity called main. If I call Toast.makeText(this, Hello World from main,
I'm sure I'm doing this inherently wrong, but I figured I'd ask anyway. I
I am using Button in main Activity. When i clicked this button it will
I'm not sure if this question has been asked before, but I can't seem
I have this code: MainActivity: public class MainActivity extends ActivityGroup In MainActivity, I use
I am trying to transfer data from one page to another but some problem
My Android app starts on an activity named LoadingActivity . This activity loads a
I called my main activity Main, and Eclipse created Main.java and res/layout/main.xml for the
So in my main activity I added an image button and it works fine.
Ive made mis-leading topic in my last question, so i open this new question

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.