0

I am connecting to an XMPP server in Android using Smack. Here is my code:

 static void openConnection() {
        try {
            if (null == connection || !connection.isAuthenticated()) {
                XMPPTCPConnectionConfiguration.Builder configuration = XMPPTCPConnectionConfiguration.builder();
                configuration.setHost(SERVER_HOST);
                configuration.setPort(SERVER_PORT);
                configuration.setServiceName(SERVICE_NAME);
                configuration.setUsernameAndPassword(new TinyDB(context.getApplicationContext()).getString("username"), new TinyDB(context.getApplicationContext()).getString("password"));
                configuration.setDebuggerEnabled(true);

                connection = new XMPPTCPConnection(configuration.build());
                connection.setUseStreamManagement(true);
                connection.setUseStreamManagementResumption(true);

                ReconnectionManager reconnectionManager = ReconnectionManager.getInstanceFor(connection);
                reconnectionManager.enableAutomaticReconnection();
                reconnectionManager.setReconnectionPolicy(ReconnectionManager.ReconnectionPolicy.RANDOM_INCREASING_DELAY);

                connection.connect();
                connection.login();
            }
        } catch (XMPPException xe) {
            xe.printStackTrace();
        } catch (SmackException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

So when I call openConnection() should I do that in an AsyncTask or is that not necessary? I am a little confused.

hrskrs
  • 4,447
  • 5
  • 38
  • 52
anupam x
  • 779
  • 2
  • 7
  • 12

4 Answers4

1

You should manage your XMPP(TCP)Connection within an Android Service. The service state (running/stopped) should reassemble the connection state: When the service is running the connection should be established or the service should try to establish the connection (if data connectivity is available). If the service stops, then also disconnect the connection.

Flow
  • 23,572
  • 15
  • 99
  • 156
  • can you help me with that?give me a link or something where i could find some useful information about the service for the connectionstate – anupam x Aug 31 '15 at 10:55
  • Look at the code of the various open source XMPP implementations for Android. For example MAXS Transport XMPP. – Flow Aug 31 '15 at 12:57
  • i had a look at the project but i am not able to figure out which og the class is a service. I found a java class XMPPService but that does not extend the service class. Can you give me a link to its service? – anupam x Sep 04 '15 at 10:35
  • Is there a simpler way to do this.Like just adding a connection listener to xmpptcpconnection which also takes into account network activity like switching between 3g or wifi and such? – anupam x Sep 04 '15 at 11:10
  • You would still need a place to hold you XMPPConnection reference. – Flow Sep 04 '15 at 11:10
  • sorry i am new to this which is why i am asking silly questions. But cant we store the reference in a variable? – anupam x Sep 04 '15 at 11:45
  • hey i need help i looked at your code of MAXS but couldnt fathom much. I need to know how to store a XMPPConnection reference? – anupam x Sep 08 '15 at 08:02
0

Yes, as the official documentation points it out:

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

zkminusck
  • 1,230
  • 1
  • 12
  • 23
  • but i have seem some people use it without asynctask – anupam x Aug 31 '15 at 08:29
  • Of course you can do it without AsnycTask, but it's not the correct way to do it. The documentation also points out: "AsyncTasks should ideally be used for short operations (a few seconds at the most.)" – zkminusck Aug 31 '15 at 08:32
0

When i call openConnection() should i do that in an asynctask or that is not neccesary?

Shortly, YES. Everything related with networking should be moved to another thread to avoid blocking main thread. Hence doInBackground() of AsyncTask runs on another thread, which is where you should call that function.

hrskrs
  • 4,447
  • 5
  • 38
  • 52
  • Hi, I wrote xmpp connections in Application class in a Thread. when I killed the app from recent apps list, then also xmpp working y? – Prashanth Debbadwar Oct 08 '15 at 14:02
  • Because `thread` is still running. You have to stop `thread` probably to achieve what you want.`You should manage your XMPP(TCP)Connection within an Android Service`- check @Flow answer – hrskrs Oct 08 '15 at 14:28
  • I want xmpp to work when only app is opened. If I use bound service i need to write the code in all activities. Please suggest me a solution. – Prashanth Debbadwar Oct 09 '15 at 11:27
  • If you include in a activity which is extending from `Application` you wont need to include on all other activities. Or you can implement an abstract class – hrskrs Oct 09 '15 at 11:49
  • ok. Suppose I included service in application class. then when to kill the service? – Prashanth Debbadwar Oct 09 '15 at 12:15
  • http://stackoverflow.com/questions/14846687/android-stop-service-when-exit-the-app – hrskrs Oct 09 '15 at 12:32
  • I have wrote xmpp connections in a async task(listeners in onpostexecute() i.e. on Main thread) in Application class.When application is killed(I manually removed it from recent screen list) then also listeners are working why? – Prashanth Debbadwar Oct 14 '15 at 10:53
0

I chose not to use AsyncTask for my smack project after searching around.

  1. its threading model have been quite different between Android version and need to take care about, also after honeycomb, it is single thread, long blocking this will cause issue on the whole device that also use AsyncTask , xmpp and bosh can cause long blocking up to seconds/minutes
  2. AsyncTask has implicit reference to activity and such a long operation will cause memory issues, or easy memory leakage when exception handling is not proper
  3. AsyncTask 's result will be lost if reference activity got reset, but activity in Android can be reset as easy as a simple device rotation or network configuration change, too many save and restore instance to make this usable as every xmpp operation may be long task
pny_hk
  • 121
  • 2