我正在使用xmpp smack库创建一个Android聊天应用程序。我创建了一个后台服务来监听传入的聊天并使用应用程序类来初始化xmpp连接对象。 问题是聊天监听器的chatCreated功能被调用两次因此显示了重复的消息。 这是我创建连接的应用程序类
Authenticate.java
public class Authenticate extends Application {
private static final String DOMAIN = StaticVariables.chatServer;
private static final String HOST = StaticVariables.chatServer;
private static final int PORT = 5222;
static AbstractXMPPConnection connection ;
String username,password;
private boolean connected;
@Override
public void onCreate() {
super.onCreate();
}
public AbstractXMPPConnection initializeXMPPTCPConnection(String username,String password) {
Log.e("APPLICATION", "username: "+username);
Log.e("APPLICATION", "password: "+password);
Log.i("APPLCATION", "initializeXMPPTCPConnection calle:");
this.username=username;
this.password=password;
XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
configBuilder.setUsernameAndPassword(username, password);
configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
configBuilder.setResource("Android");
configBuilder.setServiceName(DOMAIN);
configBuilder.setHost(HOST);
configBuilder.setPort(PORT);
configBuilder.setDebuggerEnabled(true);
connection = new XMPPTCPConnection(configBuilder.build());
connection=connectConnection();
Log.e("APPLICATION", "initializeXMPPTCPConnection: "+connection.isConnected());
return connection;
}
public AbstractXMPPConnection getConnection(){
return connection;
}
public AbstractXMPPConnection connectConnection()
{
AsyncTask<Void, Void, AbstractXMPPConnection> connectionThread = new AsyncTask<Void, Void, AbstractXMPPConnection>() {
@Override
protected AbstractXMPPConnection doInBackground(Void... arg0) {
// Create a connection
try {
connection.connect().login();
Log.e("Application", "doInBackground: "+connection.isConnected());
//login();
connected = true;
//sendMsg();
} catch (IOException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
}
return connection;
}
@Override
protected void onPostExecute(AbstractXMPPConnection connection2) {
super.onPostExecute(connection2);
// sendMsg(message.getText().toString());
Log.e("APPLICATION", "onPostExecute: "+connection2.isConnected());
connection=connection2;
}
};
try {
connection=connectionThread.execute().get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
Log.e("Application", "connectConnection: "+connection.isConnected());
return connection;
}
public void login() {
try {
connection.login(username, password);
Log.e("APPLICATIPN", "Yey! We're connected to the Xmpp server!");
} catch (XMPPException | SmackException | IOException e) {
e.printStackTrace();
} catch (Exception e) {
}
}
}
这是我通过其调用连接实例
的代码connection=((Authenticate)getApplication()).getConnection();
听众功能
public void listenChat(String name){
ChatManager manager = ChatManager.getInstanceFor(connection);
manager.addChatListener(new ChatManagerListener() {
@Override
public void chatCreated(final Chat chat, boolean createdLocally) {
System.out.println("Created chat");
chat.addMessageListener(new ChatMessageListener() {
@Override
public void processMessage(final Chat chat, final org.jivesoftware.smack.packet.Message message) {
//This is called twice
}
}
});
}
});
}
我是android的初学者,也是xmpp的初学者。请告诉我哪里错了我怎么解决呢
答案 0 :(得分:0)
您必须在chatCreated方法中修复createdLocally用法。
如果没有像这样创建侦听器,只需添加侦听器:
public void chatCreated(Chat chat, boolean createdLocally)
{
if (!createdLocally)
{
chat.addMessageListener(new ChatMessageListener()...);
}
}