2016-07-27 78 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; 
    } 
} 
} 

感謝

相關問題