不使用XMPP发送消息

时间:2014-02-11 12:20:06

标签: android connection xmpp

public class MainActivity extends Activity {
    Button reply;
    EditText say; 
    String username;
    private ListView listView;
   XMPPConnection connection=HomeScreen.connection;

    @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      setContentView(R.layout.chatbos_listview);
      listView = (ListView) findViewById(R.id.listview);
      Intent i=getIntent();
      username=i.getStringExtra("username");
      say=(EditText)findViewById(R.id.reply);
      reply=(Button)findViewById(R.id.replybtn);    
      reply.setOnClickListener(new OnClickListener() {
          String text = say.getText().toString();

          @Override
            public void onClick(View view) {
              Message msg = new Message(username, Message.Type.chat);
                msg.setBody(text);              
                if (connection != null) {
                    connection.sendPacket(msg);
                    //messages.add(text);
                    //setListAdapter();
                }
                System.out.println(connection);

            }
        });
     }

基本上是一款聊天应用。我在启动活动中创建了连接,在此活动中,我从启动活动获取连接并将数据发送给用户。但它不起作用。请指导我。

2 个答案:

答案 0 :(得分:0)

在发送消息之前,您必须与要向其发送消息的其他用户创建聊天:http://www.igniterealtime.org/builds/smack/docs/latest/documentation/messaging.html

也会在Asyntask中发送消息,而不是在主UI上,否则您的应用程序将崩溃。

答案 1 :(得分:0)

将此代码放在onclick方法中 String text = say.getText()。toString();

相关问题