3
votes

I am creating a chat application. For this, I am testing out a XMPP server. On login, I connect and login to the test XMPP server. It is sending and receiving messages.

What I want is, when the app is closed (on destroy method called), this service should keep the connection alive and listen to messages and store them in the same database that the app is using.

I have created a service and added connection listener and packet listener to it but as soon as I close the app, I see the service running but the connection is lost.

Login Activity:

private class ProcessLogin extends AsyncTask<String, Void, String> {
    private ProgressDialog pDialog;
    String uname,password;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        uname = txtUsername.getText().toString();
        password = txtPassword.getText().toString();

        pDialog = new ProgressDialog(LoginActivity.this);
        pDialog.setTitle("Contacting Servers");
        pDialog.setMessage("Logging in ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }
    @Override
    protected String doInBackground(String... args) {

        // Create a connection
        ConnectionConfiguration connConfig = new ConnectionConfiguration(HOST,PORT);
        XMPPConnection connection = new XMPPTCPConnection(connConfig);
        String user = null;

        HostAddress address = new HostAddress(HOST);

        try {
            try {
                connection.connect();

            } catch (SmackException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Log.i("XMPPChatDemoActivity", "[SettingsDialog] Connected to " + connection.getHost());
            Log.i("XMPPChatDemoActivity",  "[SettingsDialog] Connected to "+ address.getErrorMessage());

        } catch (XMPPException ex) {
            Log.e("XMPPChatDemoActivity",  "[SettingsDialog] Failed to connect to "+ connection.getHost());
            Log.i("XMPPChatDemoActivity",  "[SettingsDialog] Connected to "+ address.getErrorMessage());
            Log.e("XMPPChatDemoActivity", ex.toString());
            //setConnect(null);
        }
        try {
            try {
                connection.login(uname, password);
                XMPPSuper.getInstance().setConnection(connection);
            } catch (SaslException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SmackException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Log.i("XMPPChatDemoActivity",  "Logged in as" + connection.getUser());




            // Set the status to available
            Presence presence = new Presence(Presence.Type.available);

            connection.sendPacket(presence);
            //setConnect(connection);

            Roster roster = connection.getRoster();
            Collection<RosterEntry> entries = roster.getEntries();
            for (RosterEntry entry : entries) {

                Log.d("XMPPChatDemoActivity",  "--------------------------------------");
                Log.d("XMPPChatDemoActivity", "RosterEntry " + entry);
                Log.d("XMPPChatDemoActivity", "User: " + entry.getUser());
                Log.d("XMPPChatDemoActivity", "Name: " + entry.getName());
                Log.d("XMPPChatDemoActivity", "Status: " + entry.getStatus());
                Log.d("XMPPChatDemoActivity", "Type: " + entry.getType());
                Presence entryPresence = roster.getPresence(entry.getUser());

                Log.d("XMPPChatDemoActivity", "Presence Status: "+ entryPresence.getStatus());
                Log.d("XMPPChatDemoActivity", "Presence Type: " + entryPresence.getType());

                Presence.Type type = entryPresence.getType();
                if (type == Presence.Type.available)
                    Log.d("XMPPChatDemoActivity", "Presence AVIALABLE");
                Log.d("XMPPChatDemoActivity", "Presence : " + entryPresence);
                user = XMPPSuper.getInstance().getConnection().getUser();
            }
        } catch (XMPPException ex) {
            Log.e("XMPPChatDemoActivity", "Failed to log in as "+  USERNAME);
            Log.e("XMPPChatDemoActivity", ex.toString());
            user=null;
            //setConnect(null);
        }
        //                 dialog.dismiss();
        catch (SmackException.NotConnectedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return user;
    }
    @Override
    protected void onPostExecute(String user) {
        try {

            if(user != null){

                // Creating user login session
                // For testing i am storing name, email as follow
                // Use user real data

                session.createLoginSession(user,"email id", "2000");

                Intent i = new Intent("com.example.tabmainactivity");
                pDialog.dismiss();
                startService(new Intent(getApplicationContext(), iFlyChatMessage.class));
                startActivity(i);
                finish();

            }else{
                // username / password doesn't match
                pDialog.dismiss();
                Toast.makeText(getApplicationContext(),
                        "Incorrect username/password", Toast.LENGTH_SHORT).show();

            }


        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

My service:

@Override
public void onCreate() {

    connection = XMPPSuper.getInstance().getConnection();
    //PingManager keep_alive = PingManager.getInstanceFor(connection);
    configureConnection(connection);
    //keep_alive.setPingInterval(100);

    if (connection != null) {

        final DatabaseHandler db = new DatabaseHandler(this);
        // Add a packet listener to get messages sent to us
        PacketFilter filter = new MessageTypeFilter(org.jivesoftware.smack.packet.Message.Type.chat);
        connection.addPacketListener(new PacketListener() {
            @Override
            public void processPacket(Packet packet) {
                org.jivesoftware.smack.packet.Message message = (org.jivesoftware.smack.packet.Message) packet;
                if (message.getBody() != null) {
                    String fromName = StringUtils.parseBareAddress(message.getFrom());
                    Log.i("XMPPChatDemoActivity ", " Text Recieved " + message.getBody() + " from " + fromName);


                    java.util.Date date = new java.util.Date();
                    Timestamp time = new Timestamp(date.getTime());
                    db.addMessage(message.getBody().toString(), fromName, "[email protected]", "1", "2000", time.toString());
                    /*org.jivesoftware.smack.packet.Message msg = new org.jivesoftware.smack.packet.Message(fromName);
                    msg.setBody("Test Successful");
                    try {
                        connection.sendPacket(msg);

                    } catch (SmackException.NotConnectedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }*/
                    // Add the incoming message to the list view
                    /*mHandler.post(new Runnable() {
                        public void run() {
                            //    setListAdapter();
                        }
                    });*/
                    //messagelist.add(new MessageClass("[email protected]","[email protected]",recipient_uid, sender_uid,message.getBody().toString(), time.toString()));


                }
            }
        }, filter);
    }

}

/** The service is starting, due to a call to startService() */
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // We want this service to continue running until it is explicitly stopped, so return sticky.
    return START_STICKY;
}



private void configureConnection(final XMPPConnection connection){
    connection.addConnectionListener(new AbstractConnectionListener()
                                     {

                                         public void connectionClosed(){

                                             connect();




                                         }
                                         public void connectionClosedOnError(Exception e)
                                         {

                                             connect();
                                         }

                                         public void reconnectionFailed(Exception e)
                                         {
                                         }
                                         public void reconnectionSuccessful(){
                                         }
                                         public void reconnectingIn(int seconds)
                                         {
                                         }
                                     }
    );
}

private void connect()
{
    ConnectionConfiguration connConfig = new ConnectionConfiguration(HOST,PORT);
    XMPPConnection connection = new XMPPTCPConnection(connConfig);

    HostAddress address = new HostAddress(HOST);

    try {
        try {
            connection.connect();

        } catch (SmackException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Log.i("XMPPChatDemoActivity", "[SettingsDialog] Connected to " + connection.getHost());
        Log.i("XMPPChatDemoActivity",  "[SettingsDialog] Connected to "+ address.getErrorMessage());

    } catch (XMPPException ex) {
        Log.e("XMPPChatDemoActivity",  "[SettingsDialog] Failed to connect to "+ connection.getHost());
        Log.i("XMPPChatDemoActivity",  "[SettingsDialog] Connected to "+ address.getErrorMessage());
        Log.e("XMPPChatDemoActivity", ex.toString());
        //setConnect(null);
    }
    try {
        try {
            connection.login(USERNAME,PASSWORD);
            XMPPSuper.getInstance().setConnection(connection);
        } catch (SaslException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SmackException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Log.i("XMPPChatDemoActivity",  "Logged in as" + connection.getUser());




        // Set the status to available
        Presence presence = new Presence(Presence.Type.available);

        connection.sendPacket(presence);
        //setConnect(connection);

        Roster roster = connection.getRoster();
        Collection<RosterEntry> entries = roster.getEntries();
        for (RosterEntry entry : entries) {

            Log.d("XMPPChatDemoActivity",  "--------------------------------------");
            Log.d("XMPPChatDemoActivity", "RosterEntry " + entry);
            Log.d("XMPPChatDemoActivity", "User: " + entry.getUser());
            Log.d("XMPPChatDemoActivity", "Name: " + entry.getName());
            Log.d("XMPPChatDemoActivity", "Status: " + entry.getStatus());
            Log.d("XMPPChatDemoActivity", "Type: " + entry.getType());
            Presence entryPresence = roster.getPresence(entry.getUser());

            Log.d("XMPPChatDemoActivity", "Presence Status: "+ entryPresence.getStatus());
            Log.d("XMPPChatDemoActivity", "Presence Type: " + entryPresence.getType());

            Presence.Type type = entryPresence.getType();
            if (type == Presence.Type.available)
                Log.d("XMPPChatDemoActivity", "Presence AVIALABLE");
            Log.d("XMPPChatDemoActivity", "Presence : " + entryPresence);
        }
    } catch (XMPPException ex) {
        Log.e("XMPPChatDemoActivity", "Failed to log in as "+  USERNAME);
        Log.e("XMPPChatDemoActivity", ex.toString());
        //setConnect(null);
    }
    //                 dialog.dismiss();
    catch (SmackException.NotConnectedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

Any idea how to go about it?

1
can you post your code what you tried?Rakesh Kalashetti
@Rakesh I have added my code. Please have a look. I am a bit new so the code might be really bad qualitykerry

1 Answers

1
votes

when started your application start the service

add connection listener and packet listener inside AsyncTask's onPostExecute() method.

and execute AsyncTask inside service onCreate() method

executing AsyncTask inside service it keeps the task work continuously up to service alive and also xmpp connection.

i hope it will help you

it works for me...