java – 如何有效地使用android的持久XMPP
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 如何有效地使用android的持久XMPP,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4355字,纯文字阅读大概需要7分钟。
内容图文
我想开发一个即时消息应用程序. GCM,用于推送数据是一种流行的(和有效的)方式,如果你在Android上,但由于以下原因我没有使用它:
>它删除超过100个未传送的邮件.
>它不适用于没有谷歌应用程序的设备.
相反,我决定建立一个传统的XMPP服务器(openFire),我正在使用Smack api(TCP连接)进行连接.到目前为止,它进展顺利,但我有一些担忧.
这是我写的一个小测试代码(它在服务中运行):
Log.d("TAG","service has started");
SmackConfiguration.setDefaultPacketReplyTimeout(10000);
XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
.setUsernameAndPassword("admin", "football100")
.setServiceName("harsh-pc")
.setHost("192.168.0.200")
.setPort(5222).setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.build();
final AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
try {
conn2.connect();
conn2.login();
Presence presence = new Presence(Presence.Type.available);
presence.setStatus("online");
// Send the packet (assume we have an XMPPConnection instance called "con").
conn2.sendStanza(presence);
} catch (SmackException | IOException | XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("TAG", e.toString());
}
StanzaFilter filter=new StanzaFilter() {
@Override
public boolean accept(Stanza stanza) {
return true;
}
};
StanzaListener listener= new StanzaListener() {
@Override
public void processPacket(Stanza packet) throws SmackException.NotConnectedException {
Log.d("TAG","recevied stuff");
ChatManager chatmanager = ChatManager.getInstanceFor(conn2);
Chat newChat = chatmanager.createChat("harsh@harsh-pc");
newChat.sendMessage("Reply :) ");
}
};
conn2.addAsyncStanzaListener(listener,filter);
ChatManager chatmanager = ChatManager.getInstanceFor(conn2);
Chat newChat = chatmanager.createChat("harsh@harsh-pc");
try {
Random r=new Random();
// newChat.sendMessage(Integer.toString(r.nextInt()));
Thread.sleep(1500);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("TAG",e.toString());
}
}
}).start();
final Thread sleeper=new Thread(new Runnable() {
@Override
public void run() {
for(;;){
try {
Thread.sleep(100000);
Log.d("TAG","SLEEPI");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});sleeper.start();
请注意此代码段的最后一部分.我必须运行一个无限循环,以便我可以继续监听传入的数据包(如果我不包含此代码,我无法拦截任何传入的数据包).
我的问题是:
>这种方法会占用大量电池吗?
>这种方法会阻止设备睡觉吗?
>有没有更好的方法来完成任务(不使用GCM)?
>有没有办法将GCM与OPENFIRE集成?
解决方法:
我们过去有同样的要求,我们在我们的应用程序中提供了外部聊天功能,我们使用了XMPP [使用aSmack],
很清楚你已经弄清楚你需要使用外部服务才能与服务器建立持久性xmpp连接,但要确保服务应该是后台服务并使其成为粘性服务.
使用GCM或XMPP实际上取决于您设计系统的方式,您可以在应用程序中使用它们,以使其更稳定并导致更复杂!
制作如此复杂的系统仍然是一个很长的过程,你可以随时使用XMPP提供的简单解决方案.
Is this method going to take tremendous amounts of battery?
It should not!!! as you are using default ping interval here(which is
actually 30 min),it should not consume more battery. if you had used
ping interval less than 5 min then you should worried about battery
drainage problem !!!Will this method prevent the device from sleeping?
As per my understanding ,You are using TCP connection here unless there is no data on input-stream device can go into the sleep ,as soon as
some data arrive device will wake up and or you send some data on same
socket [such like heartbeat/ping] to keep cpu awake .but still this
behavior can vary as per different mobile manufacture.Is there a better way to get things done (without using GCM) ?
XMPP is always a strong alternative of gcm,as long as you know what
are you really looking for.You can always try to optimize your system
for batter output.Is there a way to integrate GCM with OPENFIRE ?
I am confused here what you really want to ask here !!! gcm(CCS) is
google implementation of XMPP server you can always create a XMPP
client using smack but to integrate GCM with Openfire means you are
looking at server to server communication ,which is out of scope of
you application requirements as per my understanding!!!!
以下是一些有用的链接,可以帮助您进行研究
Should applications using aSmack use foreground Services?
How to keep a XMPP connection stable on Android with (a)smack?
How To Create Service In Android Which Makes Persist Xmpp Connection With XMPP Server?
Keep XMPP connection (using asmack) alive on Android
内容总结
以上是互联网集市为您收集整理的java – 如何有效地使用android的持久XMPP全部内容,希望文章能够帮你解决java – 如何有效地使用android的持久XMPP所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。