Android: calling a method in an activity from a service (on same Activity)?

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?

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?

I can't find that flag or something similar, except Intent. FLAG_ACTIVITY_SINGLE_TOP. Not sure if that is the right one.

That would be a good solution but the code runs on two clients, one launches the activity from the Service, the other one launches it from an Activity, so this is where the issue arise. – James Dec 3 '10 at 22:43 I got it, it's indeed: Intent. FLAG_ACTIVITY_SINGLE_TOP.

Thanks a lot! – James Dec 3 '10 at 23:12.

Given my understanding of Activity and Service life cycles, it would appear to me that your approach of opening sockets in an Activity is going to create problems for you. If the user rotates the device, your Activity is going to be destroyed, and your socket connections along with it. Generally speaking, the Service life cycle is better suited to solving your problem.

I agree with Falmarri's suggestion. developer.android.com/guide/topics/funda....

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions