Problema XMPP chat application

  • Risposte:0
1diaboliko84
  • Post del forum: 1

25 ott 2013, 16:56:20 Tramite pagina web

Salve ragazzi,
chiedo il vostro aiuto per l'utilizzo della libreria smack.jar per creare una chat application.
ho creato la seguente classe

1public class XMPPClient implements MessageListener
2{
3 XMPPConnection connection;
4 String host, service, username, password;
5 Integer port ;
6
7 Context context;
8
9 public XMPPClient(Context ctx)
10 {
11 this.host = ctx.getString(R.string.xmpp_host);
12 this.port = Integer.parseInt((ctx.getString(R.string.xmpp_port)));
13 this.service = ctx.getString(R.string.xmpp_host);
14 this.context = ctx;
15 }
16
17 public XMPPClient(String host, Integer port, String service)
18 {
19 this.host = host;
20 this.port = port;
21 this.service = service;
22 //this.context = ctx;
23 }
24
25 public void connect() throws XMPPException
26 {
27 //XMPPConnection.DEBUG_ENABLED = true;
28 ConnectionConfiguration config = new ConnectionConfiguration(host, port);
29 connection = new XMPPConnection(config);
30 connection.connect();
31 }
32
33 public void login(String username, String password) throws XMPPException
34 {
35 connection.login(username, password);
36 Presence presence = new Presence(Presence.Type.available);
37 connection.sendPacket(presence);
38 }
39
40 public void sendMessage(String message, String to) throws XMPPException
41 {
42 Chat chat = connection.getChatManager().createChat(to, this);
43 chat.sendMessage(message);
44 }
45
46 public void disconnect()
47 {
48 connection.disconnect();
49 }
50}

l'app naviga tra varie Activity, come ad esempio la Configurazione di parametri (nome visualizzato, immagine del profilo...etc).
Vorrei sapere come fare per rendere la connection attiva per tutto il ciclo di vita dell'app. Perchè se effettuo la login in ogni activity, mi perdo alcuni messaggi.

Grazie in anticipo del vostro aiuto.

Rispondere