2012-10-13 5 views
0

내 응용 프로그램에서 나는 거기에 연결 여부를 결정할 수 있지만, 그 응용 프로그램 자체가 5 번 다시 연결하려고 할 때마다, 그리고 다시 연결할 때까지의 간격을 늘리 길 원하지만, 그렇지 않습니다. 일하면서, 그는 즉각적인 반응을 보냅니다. 당신은 잠시 동안 잠 새로운 스레드를 생성안드로이드 다시 연결 인터넷 문제

public class ReestablishConnection { 
    Application APP = new Application(); 
    boolean status; 
    int reconnectInterval = 1000; 
    int i; 

    public boolean reconnect(String URL){ 
     Thread timer = new Thread(){ 
      public void run(){ 
        for(i=1;i<6;i++){ 
         if(APP.testConnection(APP.defaultUrl()) == 0){ 
          status = false; 
         }else if(APP.testConnection(APP.defaultUrl()) == 1){ 
          status = true; 
         } 
         try { 
          sleep(reconnectInterval * i); 
         } catch (InterruptedException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
        } 


      } 
     }; 
     return status; 
    } 



} 

답변

1

하지만, (다시)를 양산 방법은 즉시 반환합니다

여기 내 재 연결 클래스입니다. postDelayed()와 함께 Handler를 사용해야합니다.

여기에 대한 대답에서보세요 :

How to run a Runnable thread in Android?

수면은 너무 자주 사용됩니다. 정확한 디자인은 연결이 가능할 때를위한 리스너를 구현하고 UI의 리스너 콜백에 응답하는 것입니다.