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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:24:51+00:00 2026-05-22T23:24:51+00:00

in this application i am running a remote service where i am starting a

  • 0

in this application i am running a remote service where i am starting a separate thread to run TCP connection… on application exit, i have to close the input stream for which i am giving a thread interrupt and changing a boolean value (stopBroadcastRequested) .. when that is satisfied in the thread block i am closing the inputstream, but it does not get closed actually… if i manually give the instream.close within the thread during normal thread execution, the input stream closes as expected.. but in the if(stopBroadcastRequested){ block of code}, it does not work..can someone tell me what is the mistake i have made…

public class BroadcastService extends Service {

    class Task implements Runnable{
    OutputStream outStream = null;
    InputStream inStream = null;

    @Override
    public void run() {
        while(!stopBroadcastRequested){
            Log.i(TAG, "Thread Task started");          
            try {
                isSocketOpen = broadCastComm.isAliveOrOpenSocket("192.168.43.2", 6000, 17, 0);

                if(isSocketOpen){
                    Log.d("SERVICE CLASS", "STARTED THREAD - Writing in output stream");

                    notificationMngr.cancelAll();
                    isShowingNotification = false;
                    outStream = broadCastComm.getCurrentOutputStream();
                    outStream.write(messageToBeSent);
                    if(Integer.valueOf(messageToBeSent[2]) != (byte)0xA0){
                        Log.e("REVERTING", "REVERTING");
                        messageToBeSent = mFormatter.formBroadCastMessage("GET_PERIPH_DATA");
                    }

                    Log.d("OUTPUT STREAM", "Message sent ->" + ByteArrayToString(messageToBeSent));
                }else{
                    connectivityStatusHandler.sendEmptyMessage(0);
                }

                Thread.sleep(3000L);

                if(isSocketOpen){

                }

            } catch (Throwable t) { 
                Log.e(TAG, "Failed to retrieve data in thread", t);
            }
            Log.d("SERVICE CLASS", "End of THREAD");

        }

        if(stopBroadcastRequested){
            Log.e("SERVICE", "STOPPED THREAD");
            try {
                Log.e("*****THREAD", "CLOSED INPUT STARTED");
                if(inStream != null)
                    inStream.close();

                Log.e("*****THREAD", "CLOSED INPUT CLOSED");
                outStream.flush();
                outStream.close();
                Log.e("*****THREAD", "CLOSED OUTPUT");
            } catch (Exception e) {
                Log.e("THREAD", "FAILED TO CLOSE STREAMS");
            }
        }
    }

    public synchronized void stopThread(){
        stopBroadcastRequested = true;
    }

}


@Override
public void onDestroy() {
    super.onDestroy();
    Log.e(TAG, "Service destroying");
    stopBroadcastRequested = true;
    serviceThread.interrupt();


    cleanNotifications();
    broadCastComm.clearConnections();
    //dbHandler.removeCallbacks(dbUpdater);
    try {
        dbHelper.cleanup();
    } catch (Exception e) {
        Log.e("SERVICE", "Failed to clear DB connections");
    }
}

}

  ------LOG
05-30 19:28:03.878: ERROR/BroadcastService(20288): Failed to retrieve data in thread
05-30 19:28:03.878: ERROR/BroadcastService(20288): java.lang.InterruptedException
05-30 19:28:03.878: ERROR/BroadcastService(20288):     at java.lang.VMThread.sleep(Native Method)
05-30 19:28:03.878: ERROR/BroadcastService(20288):     at java.lang.Thread.sleep(Thread.java:1213)
05-30 19:28:03.878: ERROR/BroadcastService(20288):     at java.lang.Thread.sleep(Thread.java:1195)
05-30 19:28:03.878: ERROR/BroadcastService(20288):     at      com.RBEI.TTApp.BroadcastService$Task.run(BroadcastService.java:126)
05-30 19:28:03.878: ERROR/BroadcastService(20288):     at java.lang.Thread.run(Thread.java:1019)
05-30 19:28:03.878: DEBUG/SERVICE CLASS(20288): End of THREAD
05-30 19:28:03.878: ERROR/SERVICE(20288): STOPPED THREAD
05-30 19:28:03.878: ERROR/*****THREAD(20288): CLOSED INPUT STARTED
05-30 19:28:03.882: ERROR/*****THREAD(20288): CLOSED INPUT CLOSED
05-30 19:28:03.882: ERROR/*****THREAD(20288): CLOSED OUTPUT
  • 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-22T23:24:52+00:00Added an answer on May 22, 2026 at 11:24 pm

    Sorry guys, the issue was that i was not unbinding the service properly which was causing all these issue… was not using the proper context for unbinding within the tabactivity.. had to use getApplicationContext() instead… 🙂

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

Sidebar

Related Questions

I have a web application running on Windows IIS. This app has a database
I occasionally have some long running AJAX requests in my Wicket application. When this
I have a windows form without running it in an application. this is the
I've created an application in C# which I run as a Windows Service. This
Right now I have a Java application running WMI querying event logs, this is
I have a thick GUI application (possibly running as a service, but also just
I have an application server (JBoss, but this also happens in Tomcat) running as
I get this exception on my Windows 7 64bit in application running in VS
My application keeps running into Timeout Expired SqlExceptions. The thing is that this query
Started working on a new application this week that is running the latest rails

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.