如何处理Asmack中的临时例外

时间:2014-10-20 17:42:46

标签: android asmack

我建立了一个基于aSmack的应用程序,显然关于连接的每一件事都在服务中运行,因此保持其连接Alive是非常重要的,因为我们知道在后台运行的服务可能会因电话的低端源而被杀死(通常是Ram),所以使用服务上的START_STICKY标志,它会以null意图重新启动。

现在我想知道手机上是否没有网络,或者突然发生意外的临时异常,(并且由于服务重新启动,reconnectionManager尚未设置),因此应用程序必须重新启动以检索其连接。我的问题是如何处理这些例外情况?我知道我可以做这样的事情:

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

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

但这是不专业的imo,我知道有一种方法可以确定临时例外但不幸的是我无法记住或找到它们。任何信息表示赞赏。非常感谢

2 个答案:

答案 0 :(得分:1)

所以这就是我所做的,完美的工作,每件事都算在内。

这是每次启动时我的服务都会发生的事情

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);

                    }
          }});

        }

答案 1 :(得分:0)

  

我的问题是如何处理这些例外?

执行重新连接。我知道的大多数应用程序都会在几秒钟后安排重新连接。