简体   繁体   English

Android服务架构问题

[英]Android service architecture question

I'm new to Android and I'm porting an iPhone app I did. 我是Android的新手,正在移植我做过的iPhone应用程序。 I've been reading up on Android services and they look very useful for the network component of my app. 我一直在阅读Android服务,它们对于我的应用程序的网络组件非常有用。 I assume there is a common pattern for what I'm trying to do, so here are the details: 我假设我要尝试的操作有一个通用模式,所以这里是详细信息:

  1. User1 and User2 agree to enter a virtual room. 用户1和用户2同意进入虚拟房间。
  2. They send messages to each other and get notified when they receive a messages. 他们彼此发送消息,并在收到消息时得到通知。

I'm using a remote server to handle the communication. 我正在使用远程服务器来处理通信。 So basically, I need three methods. 所以基本上,我需要三种方法。 One to add the users to a room, one to send messages and one to get messages. 一种将用户添加到房间,一种用于发送消息,一种用于获取消息。 Nothing special. 没什么特别的。

What I'd like to do is have one service that accepts a few paramaters. 我想做的是提供一项接受一些参数的服务。 One being which web service method to call, the rest being the parameters that need to be passed to the respective web services. 一种是要调用的Web服务方法,其余的是需要传递给相应Web服务的参数。 I'd prefer this approach to having a different service for each method call. 我希望这种方法为每个方法调用提供不同的服务。 Any suggestions on the best approach? 关于最佳方法有什么建议吗? I'm not clear on how to pass parameters to services. 我不清楚如何将参数传递给服务。

Btw, this is not a question about polling, callbacks or broadcasting. 顺便说一句,这不是关于轮询,回调或广播的问题。 I'm aware of those. 我知道这些。 I really want to know if it's feasible to pass a variable amount of parameters to a service, or it there is perhaps something else I should look into. 我真的很想知道将可变数量的参数传递给服务是否可行,或者我应该研究一下其他内容。

Sure, read about Intent s, Context.sendBroadcast() and BroadcastReceiver s. 当然,请阅读有关IntentContext.sendBroadcast()BroadcastReceiver的信息。 It's quite easy. 这很容易。 Sample from out code: 样本中的代码:

Intent i = new Intent("com.domain.app.intent.STATUS");
i.putExtra("APP", "com.domain.app.android.ActivityOrServiceOfTarget");
i.putExtra("STATUS", "some variable data, e.g. a String");
sendBroadcast(i);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM