多个活动之间如何的Andr​​oid绑定服务可以区分? [英] How android bound service can differentiate between multiple activities?

查看:179
本文介绍了多个活动之间如何的Andr​​oid绑定服务可以区分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个聊天客户端中,我有一个服务,这是从XMPP服务器不断地听。我有一个关于这项服务的体系结构的几个问题。

  1. 我已阅读服务类在Android上开发页面的文件中说的onCreate()将被调用一次,当服务运行,第一次这就是为什么我写在连接到服务器code该服务的onCreate()方法。是否确定?

  2. 当我点击这是我的朋友在我的客户活动的一个新的活动将开启,这将绑定到该服务收听和发送聊天消息的名字一个ListView元素。我想知道服务是如何区分一个特定的消息是哪个活动?因为如果我有1个多朋友聊天将有超过1活性及服务必须区分哪些邮件是哪个活动。我需要知道我怎样才能实现这个机制?如果有任何教程这也可能是非常有益的或者描述一下做我需要做的,实现这一机制?

解决方案
  

是否正常?

这是不可能的,在抽象的回答。没有什么明显不妥的做法。

  

我需要知道我怎样才能实现这种机制?

说实话,那个UI的声音......别扭。话虽这么说,你可以有活动提供一个监听器或回调对象的服务,该服务使用路由信息返回到该活动。

I am developing a chat client in which I have a Service which is listening continuously from XMPP server. I have few questions regarding the architecture of this service.

  1. I have read in the documentation of Service class on Android Development Page that onCreate() will be called once when the service run for the first time thats why I have written the connection to the server code in the service onCreate() method. Is it OK?

  2. When I click on a ListView element which is the name of my friend in my client activity a new activity will open which will bind to the service for listening and sending chat messages. I want to know how a service can differentiate that a specific message is for which activity? because If I am having chat with more than 1 friends there will be more than 1 activites and the service has to differentiate that which message is for which activity. I need to know how can I implement this mechanism ? It could be very helpful if there is any tutorial for this or describe what do I need to do to implement this mechanism?

解决方案

Is it OK?

That is impossible to answer in the abstract. There is nothing obviously wrong with that approach.

I need to know how can I implement this mechanism ?

To be honest, that UI sounds... awkward. That being said, you can have the activity supply a listener or callback object to the service, which the service uses to route messages back to that activity.

这篇关于多个活动之间如何的Andr​​oid绑定服务可以区分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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