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