aSmack as a service

泄露秘密 提交于 2019-12-05 11:37:01

i guess this example at the given link would give you the idea for making it a service. http://android.codeandmagic.org/small-test-of-asmack-xmpp-client-library/

You need to utilise the Android Service Framework.

You could check out GTalk SMS source as they utilize a service and is open source. (Main Service is the service they use to handle the connection etc. ) though it is also very complicated.

I would highly recommend you check out the basics of utilizing a service in Android.

Remember a service does not create a new thread, everything is still done on the UI thread so if you want to perform long running tasks in the background then you'll also need to implement an asynctask or executor service.

Old question, but I'll put my answer anyways.
You need to create a service, start it and put your code of aSmack connection in the service, not in any activity. The service will retain the connection even when the app is not in foreground. I am using this method in one of my client's app and it's working great.

Also, make sure you use a Handler or AsyncTask in the service to create the socket connection in another non-UI thread. Android will not allow you to create the connection in UI thread anyways.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!