The best way for a Service that starts an Activity to contact it

I have a service that listens on a socket. Upon receipt of a specific input, it must create an action. Upon receiving another input, he must kill this action. For some time I struggled to associate the service with activity through AIDL ( http://developer.android.com/guide/developing/tools/aidl.html ), but this seems inefficient. I think that AIDL is only effective when the process you want to talk about is a service, not when it's activity? I would like some tips or suggestions on how to solve my problem.

Greetings

+6
android aidl
source share
1 answer

I have a service that listens on a connector. Upon receipt of a specific input, this is the creation of activity.

Please configure this. Services should not start, except in very unusual circumstances (for example, the socket is a SIP connection and you are creating a VOIP client). The appearance of activity interrupts the user in what they do.

If you receive another input, kill this activity.

The only scenario that I saw when this is a valid pattern is to reject the screen in call mode when the other side hangs a line. If you are creating a VOIP client, your proposed template should be fine, but otherwise, please review if activity disappears in the middle of the user using it.

I think that AIDL is only effective when the process you need to talk about is a service, not when it's activity?

No, it works in the opposite direction, but usually only if this action starts the service and becomes attached to it. More importantly, AIDL is for interprocess communication only.

I like some directions or suggestions on how to solve my problem.

You have not provided sufficient information about the nature of the message to give you a comprehensive answer. What, in fact, is a service trying to talk about its activities? Is activity also an attempt to contact the service?

The recommended template for continuous communication from an action to a service is to use a local binding template. You will find an example of this in your SDK samples, and you can find it here .

Then the service has options for customer feedback: through a callback (for example, Handler in the response provided by Mr. Smilyanich) or through an Intents broadcast. In the case of a callback, the activity must be tied to the service in order to access the API to provide the callback object. Then the service will hold this object and call methods on it during key events.

If your service does most of the work on the background thread, you need to make sure that your user interface actions are performed in the user interface thread. Handler is one approach to this.

+9
source share

All Articles