简体   繁体   中英

How to keep XMPP connection alive in the App?

I am currently working on an application that uses asmack lib to connect to a XMPP Server. This application basically includes Sending/receiving messages, changing statuses, etc.

At the moment, the XMPP connection lives inside the application and isn't in some sort of background service. So now I'm wondering, is it better to keep the connection alive using a service or just keeping the connection alive when my application is actually running.

This is taking in consideration that I want to stay connected to the XMPP Server the entire time when my application is running background and when user come back to any activity having XMPP connection. i did like this, if it comes to main activity ( means where i am connecting with credentials ) re-connecting the XMPP connection with same credentials. but i am facing problem that when i stay for some time in contacts view, the connection is getting closed after some time if that activity resumes back getting foreclose at connection ( ie null pointer exception ).Here it is not possible to re-connect the connection.

So in a way I'm asking if it's better to (re)connect/log-in as soon as my Activity is brought to the foreground/started or is it better to connect once inside a service and just keep this connection alive?

If service creation is better way, How to create a from fragments and how to create XMPP connection and i have to do log-in and log-out by using buttons .how to maintain these options in service.

Thanks in advance,

If you want to be connected to the XMPP server all the time, Service is the way to go.

So once the user log in, you can start the communication service and keep it running and when the user log out stop the service

You can show notifications from your service that will open the activity when it is clicked.

If you have simple communication (eg pass few commands) between your service and your activity you can do this using LocalBroadcastManager , but if your communication is more complex (eg activity listen to event in the service) consider creating a service binder that is used from the activity

This an example skeleton of a service that supports bind

public class MyService extends Service {
    private final IBinder binder = new ServiceBinder();

   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {
      onHandleIntent(intent);
      return START_STICKY;
   }

   protected void onHandleIntent(Intent intent) {
       // handle intents passed using startService()
   }

   @Override
   public IBinder onBind(Intent intent) {
    return binder;
   }

   public class ServiceBinder extends Binder {
        MyService getService() {
            return MyService.this;
        }
    }
}

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