2017-07-09 3 views
0

私は今massegesを送ることができますし、私の次のステップは、着信メッセージを受信することです。私は多くの方法を探していました。彼らのほとんどは、私の痴漢のバージョンでは、侮辱されているか、利用できません。 smack 4.2.0を使用しています。誰かが、どのようにどこで私はすべての着信メッセージをキャッチするリスナーを実装できるか教えてくれますか? ここに私のコード:ピシャリ4.2受信メッセージは

MainActivity.class

パッケージcom.example.saddam.xmpp3。 IncomingChatMessageListener:あなたは次のインタフェースを使用することができますスマック4.2では

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

import org.jivesoftware.smack.AbstractXMPPConnection; 

import org.jxmpp.stringprep.XmppStringprepException; 

public class MainActivity extends AppCompatActivity { 
    Button b1; 
    TextView chat; 
    final connectXmpp con = new connectXmpp(); 

    public MainActivity() throws XmppStringprepException { 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 


     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     con.execute(); 

     b1 = (Button) findViewById(R.id.button); 
     chat = (TextView) findViewById(R.id.editText); 


     b1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       AbstractXMPPConnection conn=con.getConnection(); 
       con.sendMessage(conn,chat); 


      } 
     }); 


    } 
    } 

connectXmpp.class

import org.jivesoftware.smack.packet.Message; 
import org.jivesoftware.smack.tcp.XMPPTCPConnection; 
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration; 
import org.jxmpp.jid.EntityBareJid; 
import org.jxmpp.jid.impl.JidCreate; 
import org.jxmpp.stringprep.XmppStringprepException; 


import java.io.IOException; 


/** 
* Created by saddam on 08.07.2017. 
*/ 

public class connectXmpp extends AsyncTask<Void,Void,Void> { 
    static AbstractXMPPConnection conn2= null; 


    final EntityBareJid jid = JidCreate.entityBareFrom("[email protected]"); 

    public connectXmpp() throws XmppStringprepException { 
    } 

    @Override 
    protected Void doInBackground(Void... voids) { 

     XMPPTCPConnectionConfiguration config = null; 
     try { 
      config = XMPPTCPConnectionConfiguration.builder() 
        .setUsernameAndPassword("username", "password") 
        .setXmppDomain("dismail.de") 
        .setHost("dismail.de") 
        .setPort(5222).setKeystoreType(null) 
        .build(); 
     } catch (XmppStringprepException e) { 
      e.printStackTrace(); 
     } 
     conn2 = new XMPPTCPConnection(config); 

     try { 
      conn2.connect(); 
      conn2.login(); 


     } catch (InterruptedException e1) { 
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } catch (SmackException e1) { 
      e1.printStackTrace(); 
     } catch (XMPPException e1) { 
      e1.printStackTrace(); 
     } 




     return null; 
    } 



    @Override 
    protected void onProgressUpdate(Void... values) { 
     super.onProgressUpdate(values); 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 
    } 

    public AbstractXMPPConnection getConnection(){ 
     return conn2; 
    } 

    public ChatManager getChatMana(AbstractXMPPConnection c){ 
     ChatManager chatManager = ChatManager.getInstanceFor(c); 
     return chatManager; 
    } 

    public void sendMessage(final AbstractXMPPConnection c, TextView t){ 
     ChatManager chatManager = null; 

     Message newMessage = null; 
     chatManager = ChatManager.getInstanceFor(c); 

     try { 


      Chat chat = chatManager.chatWith(jid); 
      newMessage = new Message(); 
      newMessage.setBody(t.getText().toString()); 
      chat.send(newMessage); 


     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } catch (SmackException.NotConnectedException e) { 
      e.printStackTrace(); 
     } 

    } 

} 

答えて

2

サンプルコード:

ChatManager chatManager = ChatManager.getInstanceFor(mConnection); 
chatManager.addIncomingListener(new IncomingChatMessageListener() { 
      @Override 
      public void newIncomingMessage(EntityBareJid from, Message message, Chat chat) { 
       // Your code to handle the incoming message 
      } 
     }); 

あなたはまた、あなたのXMPPTCPConnectionConfigurationに次のステートメントを追加する必要があります。存在が偽であるか設定されていない場合は、

.setSendPresence(true) 

を、すべてあなたの着信メッセージますオフラインメッセージストレージに保存され、インターフェイスは起動されません。

これが役に立ちます。 1対1のチャットのための

+0

その作業罰金..しかし、グループチャットのために働いていません。グループ着信メッセージ用の他のコードはありますか? – user392117

+0

もちろん、この場合、MutliUserChatを使用することができます。ドキュメントは次のとおりです。https://docs.jivesoftware.com/smack/latest/javadoc/org/jivesoftware/smackx/muc/MultiUserChat.html – Ghostw4lk

関連する問題