3

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 ( i.e 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,

RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
  • You should include some code and the exception StackTrace. do you need to be connected to the XMPP server all the time (i.e. in the background) or only when your application is active in the foreground? – iTech Feb 11 '13 at 07:10
  • I need to connect XMPP service all time when the user log-in state is true, because i have to show notification when the app not in the fore-ground.. – RajaReddy PolamReddy Feb 11 '13 at 07:15

1 Answers1

6

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 (e.g. pass few commands) between your service and your activity you can do this using LocalBroadcastManager, but if your communication is more complex (e.g. 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;
        }
    }
}
Community
  • 1
  • 1
iTech
  • 18,192
  • 4
  • 57
  • 80
  • I am getting error like this 02-11 12:56:15.079: E/AndroidRuntime(12513): FATAL EXCEPTION: main, 02-11 12:56:15.079: E/AndroidRuntime(12513): java.lang.NullPointerException, at the getting connection, because when i use long time this app that connection will be lost and unable to retrive it. – RajaReddy PolamReddy Feb 11 '13 at 07:28
  • You need to provide the code, and the exception StackTrace; otherwise it is very difficult to know what is going on. – iTech Feb 11 '13 at 07:30
  • previous i am not using service, now i i will try to change that code to service like what you said above, if i get any errors let u know, and help me when i face problem in this because i don't get command on service use in android. – RajaReddy PolamReddy Feb 11 '13 at 07:33
  • How to pass User_name and User_password to service when i click on login to create xmpp connection and same time when i click on log-out i want to disconnect xmpp connection in servicse, how to manage these things, how to call function available in service from activity. – RajaReddy PolamReddy Feb 11 '13 at 08:56
  • `Intent intent = new Intent(context,MyService.class);` `intent.putExtra("username","value");` `intent.putExtra("password","value");` – iTech Feb 11 '13 at 08:57