启动活动与其通信的服务的最佳方法 [英] Best way for Service that starts Activity to communicate with it

查看:158
本文介绍了启动活动与其通信的服务的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个监听套接字的服务。当收到某些输入它要创建活动。当接收其他输入,这是要杀了这个活动。我挣扎了一会儿,使通过AIDL(http://developer.android.com/guide/developing/tool​​s/aidl.html)该活动的服务进行通信,但这似乎没有效果。我认为AIDL是当过程,是要谈的就是一种服务,而不是当它是一个活动的唯一有效?我很想对如何解决我的问题的一些指示或建议。

干杯,

解决方案
  

我有听一个服务   插座。当收到某些输入   这是创建一个活动。

请让这个配置。除了在服务不应该首发活动的非常的异常情况(例如,插座是一个SIP连接,并要创建VoIP客户端)。弹出一个活动中断,在任何他们正在做的用户。

  

当接收其他输入,它是要   杀了这个活动。

我见过的唯一的场景,这是一个有效的模式被解聘的通话屏幕时,对方挂断线路。如果要创建一个VoIP客户端,你提出的模式应该是好的,但在其他方面,请重新考虑其使用它在用户中间的活性消失。

  

我觉得AIDL是唯一有效的当   这个过程是要谈的就是   一个服务,而不是当它是一个活动?

没有,它工作在反方向也一样,但通常只有当活动是一个启动服务和结合它。更重要的是,AIDL仅用于跨进程通信。

  

我会喜欢一些指示或   如何解决的建议我   问题。

您还没有真正提供对通信的本质足够的信息给你一个全面的答案。究竟什么是服务试图告诉活动?是活动也试图与该服务进行通信?

推荐的模式为正在进行的通信从活动到服务是使用本地绑定模式。你会发现你的SDK样本中这样的例子,你可以找到一个的此处为好。

然后服务具有传递回客户端选项:通过回调(例如,处理程序由Smiljanić先生提供的答案),或通过广播意图。在回调的情况下,活动将需要绑定到服务,以获得访问一个API提供回调对象。该服务将随后守住该对象,并在关键事件调用它的方法。

如果您的服务做的主要工作在后台线程,你需要确保你的UI操作UI线程上执行得到。该处理程序是一个方法的。

I have a service that listens to a socket. When receiving certain input it is to create an activity. When receiving other input, it is to kill this activity. I have struggled for a while to make the service communicate with the activity through AIDL (http://developer.android.com/guide/developing/tools/aidl.html), but this seems to not be effective. I think AIDL is only effective when the process that is to be talked to is a service, not when it is an activity? I would love some directions or suggestions on how to solve my problem.

Cheers,

解决方案

I have a service that listens to a socket. When receiving certain input it is to create an activity.

Please make this configurable. Services should not be starting activities except in very unusual circumstances (e.g., the socket is a SIP connection and you are creating a VOIP client). Popping up an activity interrupts the user in whatever they are doing.

When receiving other input, it is to kill this activity.

The only scenario I have seen where this is a valid pattern is dismissing the in-call screen when the other party hangs up the line. If you are creating a VOIP client, your proposed pattern should be OK, but otherwise, please reconsider having the activity vanish in the middle of the user using it.

I think AIDL is only effective when the process that is to be talked to is a service, not when it is an activity?

No, it works in the reverse direction too, but usually only if the activity is the one starting the service and binding to it. More importantly, AIDL is only for cross-process communication.

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

You have not really provided enough information on the nature of the communication to give you a thorough answer. What, exactly, is the service trying to tell the activity? Is the activity also trying to communicate with the service?

The recommended pattern for ongoing communication from an activity to a service is to use the local binding pattern. You will find an example of this in your SDK samples, and you can find one here as well.

The service then has options for communicating back to the client: via a callback (e.g., the Handler in the answer supplied by Mr. Smiljanić) or via broadcast Intents. In the case of the callback, the activity would need to bind to the service in order to get access to an API to provide the callback object. The service would then hold onto that object and call methods on it during key events.

If your service is doing its primary work on a background thread, you will need to ensure that your UI operations get performed on the UI thread. The Handler is one approach to that.

这篇关于启动活动与其通信的服务的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆