2014-10-20 16 views
1

我構建了基於aSmack的應用程序,顯然關於連接的每一件事都在服務中運行,所以保持其連接非常重要,因爲我們知道服務在後臺運行,可能會因電話的低端來源(通常是Ram)而死亡,所以通過服務上的START_STICKY標誌,它會以無意的方式重新啓動。如何在Asmack中處理臨時異常

現在我想知道手機上是否沒有網絡,或者突然意外的臨時異常發生在那個時候(並且因爲服務重新啓動,重新連接管理器尚未設置),所以應用程序必須重新啓動以檢索它連接。我的問題是我如何處理這些異常?我知道我可以做一些事情是這樣的:

public void connect(){ 
try { 
       connection.connect(); 
      } catch (SmackException | IOException | XMPPException e) { 

       if(getIntent() == null){ 
        connect(); 
        return; 
       } 
       } 
} 

但是這是不專業的海事組織,我知道有一種方法來確定臨時異常,但unfortunanly我不能要麼記住或找到它們。任何信息表示讚賞。非常感謝

回答

1

所以這裏是我所做的,完美的作品,每一件事都在這個數字裏面。

這是awhat我的服務並在每次啓動

private void connect(){ 

if (!connection.isConnected()){ 


      try { 
       connection.connect(); 
      } catch (SmackException | IOException | XMPPException e) { 

       mainHandler.post(new Runnable() { 

        @Override 
        public void run() { 
          if(intent ==null){ 
         Toast.makeText(getBaseContext(),"Could not Connect to The Server , Network Problems , Retrying in 30 Seconds...", Toast.LENGTH_LONG).show(); 
         }else{ 
         Toast.makeText(getBaseContext(),"Could not Connect to The Server , Network Problems...", Toast.LENGTH_LONG).show(); 
         } 

        } 
       }); 

       if(intent == null){ 
    //When intent is null, It Means that service got Destroyed middle of app, which  
//means user has already connected and Authenticated once, but can not do it again. 
//so thats the key 

      nonMainHandler.postDelayed(new Runnable() { 

         @Override 
         public void run() { 
          connect(); 

         } 
        },30000); 
       } 


       e.printStackTrace(); 
      } 


     } 

    try { 
      if (connection.isConnected() && !connection.isAuthenticated()) { 


      try { 
       connection.login(LMApplication.userName,LMApplication.passWord); 
      } catch (SmackException | IOException | XMPPException e) { 

       mainHandler.post(new Runnable() { 

        @Override 
        public void run() { 
         if(intent != null){ 
         Toast.makeText(getBaseContext(),"Could not Login Using this Information..", Toast.LENGTH_LONG).show(); 
         } 

        } 
       }); 

       e.printStackTrace(); 

       configEnterButton(-1); 

      } 


      } 


     if(connection.isAuthenticated()){ 

      configEnterButton(100); 


      try { 
       Thread.sleep(300); 
      } catch (InterruptedException e1) { 

       e1.printStackTrace(); 
      } 


     goAhead(); 




     Log.i("XMPPChatDemoActivity", "Logged in as" + LMApplication.Raw_CurrentUser); 
     //TODO 
     // check if need to set presence from shared preferences 
     Presence p = new Presence(Presence.Type.available,"", 42, Mode.available); 
     try { 
      connection.sendPacket(p); 
     } catch (NotConnectedException e1) { 

      e1.printStackTrace(); 
     } 


     }else if(intent == null){ 

      nonMainHandler.post(new Runnable() { 

       @Override 
       public void run() { 

        if(connection.isConnected() && !connection.isAuthenticated()){ 

         try { 
          connection.login(LMApplication.userName,LMApplication.passWord); 
         } catch (XMPPException | SmackException 
           | IOException e) { 

          e.printStackTrace(); 
         } 
        } 

        if(connection.isConnected() && connection.isAuthenticated()){ 

         notifyReconnect(); 

        }else if(connection.isConnected()){ 

         nonMainHandler.postDelayed(this,10000); 

        } 
      }}); 

     } 
0

我的問題是我該如何處理這些異常?

通過執行重新連接。我知道的大多數應用程序會在幾秒鐘後安排重新連接。

+0

多數民衆贊成還是個菜鳥的答案TBH – Reza 2014-10-21 01:09:20

+0

我不知道回答什麼其他的時間。也許你可以改寫你的問題。 – Flow 2014-10-21 07:55:04

+0

當然,好吧,我會交配 – Reza 2014-10-21 15:47:15