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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:06:47+00:00 2026-06-05T20:06:47+00:00

I have a service running and listening on a socket. This service will start

  • 0

I have a service running and listening on a socket. This service will start an activity to display a video frame when it receives a “format” message over the socket each video frame is then sent in succession.

Body of MyService: (simplified and error handling removed for brevity):

 public class MyService extends Service
    {
        ....

        while (_run)
        {
            Socket s = serverSocket.accept();
            Thread connection_handler = new thread( new handleSocketConnection( s );
        }

        public class handleSocketConnection implements Runnable
        {
            boolean _run;
            Socket s;
            handleSocketConnection( Socket socket )
            {
               _run = true;
               s = socket;
            }

            public void run( )
            {
                InputStream in = s.getInputStream();
                byte bytes[] = new byte[100];

                while( _run )
                {
                    in.read( inbytes, 0, 100 );
                    if ( bytes[0] == 10 ) // if the first byte is a 10 this is a format message
                    {
                        bring_activity_foreward();
                    }
                    else // otherwise this is video frame data
                    {
                        send_activity_bytes( bytes, 100 );
                    }
                }

            }

            public void bring_activity_foreward()
            {
                Intent show_intent = new Intent( this.MyActivity.class );
                show_intent.setFlags( Intent.FLAG_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK );
                startActivity( show_intent );
            }
        }
}

All this works great causing my activity to show when I get the first format message and draw the video frame(s). However, the server sending the video frame data will send the “format message” at regular intervals to compensate for changes in display, orientation etc. If my activity is already in the foreground, the format message calls bring_activity_foreward (which calls startActivity() ) causing the Activity to receive a “onPause” immediately followed by an “onResume”. Since my activity binds to the service when it resumes, and unbinds when it pauses, my activity is constantly unbinding and re-binding to the service. This doesn’t seem very efficient.

public class ShowFrameActivity extends Activity
{
    @Override
    public void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.viewframe );
    }

    @Override onResume( )
    { 
         super.onResume();
         bindToService();
    }

    @Override onPause( )
    {
        unbindFromService();
    }

    ...

}

Is there a way to send the intent to my activity so it won’t do a “pause -> resume” if it’s already in the foreground? Keep in mind that since my startActivity is called from a service, it must include the “FLAG_ACTIVITY_NEW_TASK”.

Thanks for any comments or suggestions.

-Z

  • 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-05T20:06:49+00:00Added an answer on June 5, 2026 at 8:06 pm

    An Activity is always paused before it receives an Intent so your approach of binding/unbinding in onResume() / onPause() is always going to produce this behaviour.

    The idea from Tim seems like a good one (and actually involves overriding onNewIntent(...)) but even then, the Activity will be paused/resumed.

    Personally I would try one of two options…

    1. Use an inner BroadcastReceiver in the Activity to receive the Intent and pass the data to the Activity. In saying that, however, I’m not sure if an inner BroadcastReceiver would cause it’s Activity to pause/resume in the same way – probably not and it’s worth trying.

    2. Define a ResultReceiver class, create an instance of it in your Activity and send it via the Intent you use to start the Service

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

Sidebar

Related Questions

I have an android recurring service running in background, no Activity running in front
I have a receive thread listening with the BluetoothServerSocket accept() call. This thread will
I have a udp service which is listening on a socket for udp datagrams:
I have an Android service that is running and listening for microphone input. I
I have an OData service running on top of Oracle database. I can query
I have a WCF service running on my machine, which get lauched while I
I have a Windows Service running on a .Net 2.0 environment (NOT UPGRADABLE TO
I have a WCF service running inside a windows service on a remote machine.
I have a WCF service running, I added a reference to the service by
I have a system service running on my Windows machine that can impersonate the

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.