3

我已使用以下代码在 android 中制作聊天应用程序。借助此代码,我可以向客户端发送消息。但我需要让其他人使用我的应用程序进行通信。但目前它是不可能。另一个人需要打开他的 xmpp 客户端窗口才能看到消息。如何使用 xmpp 在两个人之间创建会话。我看过很多教程。他们没有帮助。所以我在这里发布一个问题。

SettingDialog.java



 package org.apache.android.xmpp;

import android.app.Dialog;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;

/**
 * Gather the xmpp settings and create an XMPPConnection
 */

public class SettingsDialog extends Dialog implements android.view.View.OnClickListener {
    private XMPPClient xmppClient;
public SettingsDialog(XMPPClient xmppClient) {
    super(xmppClient);
    this.xmppClient = xmppClient;
}

protected void onStart() {
    super.onStart();
    setContentView(R.layout.settings);
    getWindow().setFlags(4, 4);
    setTitle("XMPP Settings");
    Button ok = (Button) findViewById(R.id.ok);
    ok.setOnClickListener(this);
}

public void onClick(View v) {
    //String host = getText(R.id.host);
    //String port = getText(R.id.port);
    //String service = getText(R.id.service);
    String username = getText(R.id.userid);
    String password = getText(R.id.password);
    String host,port,service;
    host="talk.google.com";
    port="5222";
    service="gmail.com";
  //  username="sabarish";
   // password="ramya";
    // Create a connection
    SmackConfiguration.setPacketReplyTimeout(500);

    ConnectionConfiguration connConfig =
            new ConnectionConfiguration("192.168.1.4 ", 5222,"ubuntu");
    XMPPConnection connection = new XMPPConnection(connConfig);

    try {
        connection.connect();
        Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost());
    } catch (XMPPException ex) {
        Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost());
        Log.e("XMPPClient", ex.toString());
        xmppClient.setConnection(null);
    }
    try {
        connection.login(username, password);
        Log.i("XMPPClient", "Logged in as " + connection.getUser());

        // Set the status to available
        Presence presence = new Presence(Presence.Type.available);
        connection.sendPacket(presence);
        xmppClient.setConnection(connection);
    } catch (XMPPException ex) {
        Log.e("XMPPClient", "[SettingsDialog] Failed to log in as " + username);
        Log.e("XMPPClient", ex.toString());
            xmppClient.setConnection(null);
    }
    dismiss();
}

private String getText(int id) {
    EditText widget = (EditText) this.findViewById(id);
    return widget.getText().toString();
}

}

xmppclient.java



  package org.apache.android.xmpp;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;

import java.util.ArrayList;

public class XMPPClient extends Activity {

    private ArrayList<String> messages = new ArrayList();
    private Handler mHandler = new Handler();
    private SettingsDialog mDialog;
    private EditText mRecipient;
    private EditText mSendText;
    private ListView mList;
    private XMPPConnection connection;

    /**
     * Called with the activity is first created.
     */
    @Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    Log.i("XMPPClient", "onCreate called");
    setContentView(R.layout.main);

    mRecipient = (EditText) this.findViewById(R.id.recipient);
    Log.i("XMPPClient", "mRecipient = " + mRecipient);
    mSendText = (EditText) this.findViewById(R.id.sendText);
    Log.i("XMPPClient", "mSendText = " + mSendText);
    mList = (ListView) this.findViewById(R.id.listMessages);
    Log.i("XMPPClient", "mList = " + mList);
    setListAdapter();

    // Dialog for getting the xmpp settings
    mDialog = new SettingsDialog(this);

    // Set a listener to show the settings dialog
    Button setup = (Button) this.findViewById(R.id.setup);
    setup.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            mHandler.post(new Runnable() {
                public void run() {
                    mDialog.show();
                }
            });
        }
    });

    // Set a listener to send a chat text message
    Button send = (Button) this.findViewById(R.id.send);
    send.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            String to = mRecipient.getText().toString();
            String text = mSendText.getText().toString();

            Log.i("XMPPClient", "Sending text [" + text + "] to [" + to + "]");
            Message msg = new Message(to, Message.Type.chat);
            msg.setBody(text);
            connection.sendPacket(msg);
            messages.add("You"+ ":");
            messages.add(text);
            setListAdapter();
        }
    });
}

/**
 * Called by Settings dialog when a connection is establised with the XMPP server
 *
 * @param connection
 */
public void setConnection
        (XMPPConnection
                connection) {
    this.connection = connection;
    if (connection != null) {
        // Add a packet listener to get messages sent to us
        PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
        connection.addPacketListener(new PacketListener() {
            public void processPacket(Packet packet) {
                Message message = (Message) packet;
                if (message.getBody() != null) {
                    String fromName = StringUtils.parseBareAddress(message.getFrom());
                    Log.i("XMPPClient", "Got text [" + message.getBody() + "] from [" + fromName + "]");
                    messages.add(fromName + ":");
                    messages.add(message.getBody());
                    // Add the incoming message to the list view
                    mHandler.post(new Runnable() {
                        public void run() {
                            setListAdapter();
                        }
                    });
                }
            }
        }, filter);
    }
}

private void setListAdapter
        () {
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.multi_line_list_item,
            messages);
    mList.setAdapter(adapter);
}

}

4

0 回答 0