最好的方法从服务器发送数据到Android设备 [英] Best approach to send data from a server to an Android device

查看:162
本文介绍了最好的方法从服务器发送数据到Android设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序,需要双向通信与服务器。通过这一点,我的意思是在服务器或设备可以在任何时间发送消息,并在消息之间的任意量的时间。从设备将数据发送到服务器是一种常见的,我觉得很好理解的任务,但我没有那么肯定,最好的办法是向相反的方向,从服务器到设备去了。

I am developing an Android app that needs to communicate bi-directionally with a server. By that, I mean either the server or the device can send a message at any time, with an arbitrary amount of time in between messages. Sending data from the device to the server is a common and I think well understood task, but I'm not as sure what the best approach is to go in the opposite direction from the server to the device.

我觉得有定期的设备轮询服务器可能是由于延迟一个坏主意,并在电池电量的消耗,但我很愿意考虑这个选项。我目前的计划是通过电子邮件到SMS桥从服务器发送短信,并让我的应用程序运行,以接收和处理这些信息的服务。

I think having the device periodically poll the server may be a bad idea due to latency and the drain on the battery, but I'd be willing to consider this option. My plan at the moment is to send text messages from the server via an email-to-SMS bridge, and to have my app run a service to receive and handle these messages.

我的问题是,如果有这种情况下任何的最佳做法,而如果使用短信有一些,我没有考虑到负面影响。为了这个问题,我想假设用户有无限的文本数据计划,所以每个文本支付将不再是一个问题。

The question I have is if there are any best practices for this scenario, and if using text messages has some downsides that I have failed to consider. For the sake of this question, I want to assume that users have an unlimited text data plan, so paying per text won't be an issue.

推荐答案

这是什么 XMPP 的擅长。 :)

This is what XMPP excels at. :)

见的Smack Java库,或 asmack 一个Android的定制版。

See the Smack Java library, or asmack for an Android-customised version.

和一个Android应用程序,使用asmack长寿命的双向通信,请参见 BuddyDroid

And for an Android app that is using asmack for long-lived bidirectional communications, see BuddyDroid.

修改,在回答下面的评论:
在你的情况下,这个方法的好处是,你可以立即推无限量的客户端,无需短信延迟(或非保证交货)。 XMPP还为您提供连接安全,COM pression和层您自己的扩展在它之上的能力。
此外—我不知道谁是你的目标或者是否适用—这样一来,你就不需要依靠用户有无限的文本计划(当然你需要一个数据计划)。

Edit, in reply to comment below:
The advantage of this approach in your case is that you can push unlimited amounts of to the client immediately, without the latency (or non-guaranteed delivery) of SMS. XMPP also gives you connection security, compression and the ability to layer your own extensions on top of it.
In addition — and I don't know who you're targeting or whether this applies — this way you don't need to rely on the user having an unlimited text plan (but of course you need a data plan).

请参阅韵律或的 Tigase 如果你有兴趣,寻找一台服务器(有几个开源的)。

See Prosody or Tigase if you're interested and looking for a server (there are several open source ones).

不过,在重读你的问题,你似乎暗示活动率低所以也许它不值得维持(登录,检测连接丢失,重新连接)的XMPP会话无论你的应用程序。

However, having re-read your question, you seem to imply a low rate of activity so perhaps it's not worth maintaining (login, detecting connection loss, reconnecting) an XMPP session for whatever your application is.

<打击>最后。虽然你可以在Android设备上拦截来袭短信,您不可以 $ P $无法显示在用户的邮件收件箱中pvent他们。所以,你可以处理传入的短信,但你不能再告诉系统丢弃这个消息。
最近我发现这是不正确的:如果你定义一个机器人:在您的短信,监听优先属性意图过滤器,然后你会收到一个有序广播,它可以让你<一href="http://developer.android.com/reference/android/content/BroadcastReceiver.html#abortBroadcast()"相对=nofollow>取消收到SMS广播之前,它的传播到其他应用程序,如手机短信收件箱。请注意,这仅适用于Android 1.6及以上。

Finally.. while you can intercept incoming SMS messages on an Android device, you cannot prevent them from showing up in the user's messaging inbox. So you can process the incoming SMS, but you can't then tell the system to discard the message.
I recently discovered this isn't true: if you define an android:priority attribute on your SMS-listening intent-filter, you will then receive an "ordered broadcast" which allows you to cancel the incoming SMS broadcast before it's propagated to other apps, like the SMS inbox. Note that this only works on Android 1.6 and above.

这篇关于最好的方法从服务器发送数据到Android设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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