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
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...
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.
Define a ResultReceiver class, create an instance of it in your Activity
and send it via the Intent
you use to start the Service