简体   繁体   中英

Do I have to create two separate classes for smack xmpp connection, one for background listener and another for foreground listener?

I am a beginner in Java and as well as in Android. I am creating an Android chat application using xmpp smack library. My requirement is I want my xmpp connection to listen to received messages even my app is in background. I know for background processes I have to create a service. But I want my app to keep connected even my app is in background and as well is in foreground. So my question is do I have create two classes of xmpp, one for background process and another for foreground (when user is online)

Just create a simple service and make it run on background thread which will receive your message and from there you can either pass your message to the main thread(using handlers or broadcast receiver) and do the things you want on main thread or perform your background operations on the data received. Hope this is what you are asking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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