I’ve had a similar question but the answer did not entirely fix my problem.
Here is what I am trying to do: I have a Service class that loops to receive TCP connections. If a connection is received, a string can be read. It can be one of those two: “START” or “STOP”.
When “START” comes in, I create (start) a new activity which has some GUI, and also two threads (one sends UDP packets and records audio, the other one receives UDP and plays audio). This class holds 2 sockets, one AudioTrack object and one AudioRecord.
When the Service class receives a “STOP” as its data in a TCP connection, I would like to call a method inside that Activity class (not create a new one), to close those 2 sockets and call stop() and release() on the AudioTrack/AudioRecord object.
Right now, when I receive a “STOP”, I am only able to create a whole new activity, so those objects are not initialized. Is there a way please, to actually call a method inside a previously started Activity? Could I keep a reference to that activity (or intent) and simply call a method about it, or do I have to change my whole designs? Suggestions would be welcomed in that case.
Thank you,
James
You can set the
START_FLAG_SINGLE_INSTANCE(I think that’s what it’s called) in the intent flags and it will sent the intent to the already running activity. You can then get that intent in onNewIntent() in the activity.Alternatively, why not have the service manage the sockets?