2011-06-30 8 views
3

私はこの質問を見つけました:XMPP events on Android、答えは私が抱えている問題の解決方法を説明しています。しかし、私はpacketListenerを動作させる方法を理解できないようです。私はpacketCollectorsとpacketListenersを試しましたが、注意が必要です。androidのXMPPパケットをリッスンする

私はどこでパケットリスナーを置くべきかわかりません。サービスのonCreate()メソッドまたはonStartCommand()に入るか、それとも数秒ごとに実行される別のメソッドに入れるべきですか?

私の混乱は、主にリスナーの概念にあります。リスナーは常に稼働しており、パケットが受信されるとすぐにトリガーされますか?そして、リスナーが入ってくるパケットイベントを「見る」ことができるようにするにはどうしたらいいですか?

これは、これが動作するようになっての私の現在の試みです:

public class XMPPService extends Service{ 

    private static String TAG = "XMPPService"; 
    XMPPConnection connection; 
    MultiUserChat muc;  

    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void onCreate(){ 
     super.onCreate(); 
    } 

    public int onStartCommand(Intent intent, int flags, int startId){ 

     ConnectionConfiguration config = new ConnectionConfiguration("jabber.org", 5222); 
     connection = new XMPPConnection(config); 

     try 
     { 
      connection.connect(); 
      Log.i(TAG,"connected"); 
     } 
     catch (XMPPException e) 
     { 
      Log.e(TAG, "Connection Issue: ", e); 
     } 

     try 
     { 
      connection.login("user", "password"); 

      muc = new MultiUserChat(connection, "[email protected]"); 
      muc.join("nick","password");  

      Presence presence = new Presence(Presence.Type.available); 
      connection.sendPacket(presence); 
      setConnection(connection); 
     } 
     catch (XMPPException e) 
     { 
      Log.e(TAG, "Connection Issue: ", e); 
     } 

     makeNotification("started"); 

     return 1; 
    } 

    private void makeNotification(String msg){ 
     Notification notification = new Notification(R.drawable.icon, msg, System.currentTimeMillis()); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MUCTestActivity.class), 2); 
     notification.setLatestEventInfo(this, "Title", msg, contentIntent); 

     NotificationManager nmgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
     nmgr.notify(123443, notification); 
    } 


    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(TAG, "Got text [" + message.getBody() + "] from [" + fromName + "]"); 
        } 
        else 
        { 
         Log.i(TAG,"null"); 
        } 
       } 
      }, filter); 
     } 
    } 

} 
+0

私も同じ問題に直面しています。この問題が解決した場合はお知らせください。私は、アクティビティに関係なく、アプリケーションからイベント通知(メッセージ、フレンドリクエスト、接続イベントなど)を知りたい。 @Atomix – Roster

答えて

1

PacketListener部分が右に見えます。 smacks接続(リーダスレッド)に接続され、フィルタに一致するパケットが到着した場合に接続によって呼び出されます。あなたはコンセプトを持っています。 START_STICKY(ここでは値ではなく定数を使用してください)を返すことは、サービスと接続を維持するのにも役立ちます。

オンラインで接続のJIDが表示されますか。 JIDの名簿に載っていなくても常にJIDにメッセージを送信できるかどうかはわかりません。 smackでデバッグを有効にしてみてください:Connection.DEBUG_ENABLED = true。 asmackをお持ちの場合は、すべてのデバッグメッセージがDBMSログに記録されます。また、ブレークポイントを持つエミュレータとエクリプスは、問題の分析に役立つ強力なツールです。

関連する問題