2016-07-27 73 views
0

我正在研究Android中的聊天(1对1)应用程序。我在服务器上使用Smack api和jabber。如何将xmpp监听器添加到android中的服务?

建立连接

使用的AsyncTask我做connection.connect().login();在应用程序启动的,或者当用户会话可用。 XMPP Connection由匕首提供。

XMPP监听

  1. 连接侦听(重新连接上断开)
  2. 消息侦听器(进来的消息,存在下),发送通知时应用不是前景。

我最初的想法是设置一个包含消息侦听器的IntentService,但我不确定如何将IntentServices设置为长期运行的任务,无限期地在应用上运行。

谢谢。

回答

0

在服务中,您可以检查连接并断开连接,然后尝试再次尝试登录。请参阅下面的代码。希望这会帮助你解决你的问题。

public class MyService extends Service { 

public static final String ACTION_LOGGED_IN = "chatapp.loggedin"; 
private final LocalBinder binder = new LocalBinder(); 
XMPP xmppConnection; 
private MessageHandler messageHandler; 
private static String TAG = "MyService"; 


private void onLoggedIn() { 

    FromMatchesFilter localFromMatchesFilter = null; 
    try { 
     localFromMatchesFilter = new FromMatchesFilter(JidCreate.domainBareFrom(Domainpart.from("[email protected]" + Common.SERVER_HOST)), false); 
    } catch (XmppStringprepException e) { 
     e.printStackTrace(); 
    } 

    if(messageHandler == null){ 
     messageHandler = new MessageHandler(this); 
    } 

    XMPP.getInstance().addStanzaListener(this, messageHandler); 

    XMPP.getInstance().addChatListener(this, new ChatManagerListener() { 
     @Override 
     public void chatCreated(Chat chat, boolean createdLocally) { 

     } 
    }); 

    XMPP.getInstance().configureSrvDeliveryManager(this); 

    if(XMPP.getInstance().isFileTransferNegotiatorServiceEnabled()){ 
     PurplkiteLogs.logError(TAG, "File Transfer Service is enabled"); 
    } 
    FileTransferNegotiator.IBB_ONLY = true; 

} 


public static boolean isMyServiceRunning(Context context) { 
    ActivityManager manager = (ActivityManager) context 
      .getSystemService(Context.ACTIVITY_SERVICE); 
    for (RunningServiceInfo service : manager 
      .getRunningServices(Integer.MAX_VALUE)) { 
     if (MyService.class.getName().equals(
       service.service.getClassName())) { 
      return true; 
     } 
    } 
    return false; 
} 

public XMPP XMPP() { 
    return this.xmppConnection; 
} 

public IBinder onBind(Intent paramIntent) { 
    return this.binder; 
} 

Handler holdConnectionHandler = new Handler() { 

    public void handleMessage(android.os.Message msg) { 
     PurplkiteLogs.logInfo(TAG, "Service Handler Running"); 
     checkUserLogin(); 
     holdConnectionHandler.sendEmptyMessageDelayed(0, 10 * 1000); 
    } 
}; 

private void checkUserLogin() { 
    String user = AppSettings.getUser(MyService.this); 

       XMPPTCPConnection connection = XMPP.getInstance().getConnection(MyService.this); 
       if(connection != null){ 
        connection.disconnect(); 
       } 
       if(XMPP.getInstance().isConnected()){ 
        XMPP.getInstance().close(); 
       } 
       final String user = AppSettings.getUser(MyService.this); 
       final String pass = AppSettings.getPassword(MyService.this); 
       final String username = AppSettings.getUserName(MyService.this); 
       try { 
        XMPP.getInstance().login(user, pass, username); 
       } catch (XMPPException e) { 
        e.printStackTrace(); 
       } catch (SmackException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } catch (PurplKiteXMPPConnectException e) { 
        e.printStackTrace(); 
       } 
} 


public void onCreate() { 
    super.onCreate(); 
    PurplkiteLogs.logDebug(TAG, "in onCreate of Service"); 
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
    StrictMode.setThreadPolicy(policy); 

    this.xmppConnection = XMPP.getInstance(); 
    holdConnectionHandler.sendEmptyMessage(0); 


    registerReceiver(new BroadcastReceiver() { 
     public void onReceive(Context paramAnonymousContext, 
           Intent paramAnonymousIntent) { 
      MyService.this.onLoggedIn(); 
     } 
    }, new IntentFilter("chatapp.loggedin")); 

    if ((AppSettings.getUser(this) != null) 
      && (AppSettings.getPassword(this) != null)) { 
     final String user = AppSettings.getUser(this); 
     final String pass = AppSettings.getPassword(this); 
     final String username = AppSettings 
       .getUserName(MyService.this); 

     new Thread(new Runnable() { 
      public void run() { 
       try { 
        XMPPTCPConnection connection= MyService.this.xmppConnection.getConnection(MyService.this); 
        if(connection == null || !connection.isAuthenticated()){ 
         MyService.this.xmppConnection.login(user, pass, 
           username); 
        } 
       } catch (XMPPException e) { 
        PurplkiteLogs.logError(TAG, "Error in Login"); 
        e.printStackTrace(); 
       } 
       catch (SmackException e) { 
        PurplkiteLogs.logError(TAG, "Error in Login"); 
        e.printStackTrace(); 
       } catch (IOException e) { 
        PurplkiteLogs.logError(TAG, "Error in Login"); 
        e.printStackTrace(); 
       } catch (InterruptedException e) { 
        PurplkiteLogs.logError(TAG, "Error in Login"); 
        e.printStackTrace(); 
       } catch (PurplKiteXMPPConnectException e) { 
        PurplkiteLogs.logError(TAG, "Error in Login"); 
        e.printStackTrace(); 
       } 
       MyService.this.sendBroadcast(new Intent(
         "chatapp.loggedin")); 
       return; 
      } 
     }).start(); 
    } 

} 

public class LocalBinder extends Binder { 
    public LocalBinder() { 
    } 

    public MyService getService() { 
     return MyService.this; 
    } 
} 
} 

感谢

相关问题