2014-12-31 3 views
0

내가하려는 것은 활동이 시작될 때 응용 프로그램이 서버에 ping을 보내 온라인 상태인지 확인하는 것입니다. 현재 문제는 모든 서버에 대해 ping을 수행 할 때까지 응용 프로그램이 멈추고 사용할 수 없게됩니다.백그라운드에서 Android 작업 또는 메소드를 실행 하시겠습니까?

Thread t = new Thread(new Runnable() { 
      public void run() { 
     //ping stuff 
}}; 
t.start(); 

그리고 그것은 동결 문제를 제거하지만, 아무것도 핑 실제로 될 것 같지 않습니다 : 그래서 내가 지금 가지고있는 것은. pining 후 텍스트 뷰를 "서버 상태"에서 "서버 상태 온라인"또는 "서버 상태 오프라인"으로 변경하는 중입니다.

Runtime runtime = Runtime.getRuntime(); 
       try{ 
        Process mIpAddrProcess = runtime.exec("/system/bin/ping -c 1 sfb.noip.me"); 
        int mExitValue = mIpAddrProcess.waitFor(); 
        Process mIpAddrProcess2 = runtime.exec("/system/bin/ping -c 1 sg.lbsg.net"); 
        int mExitValue2 = mIpAddrProcess2.waitFor(); 
        Process mIpAddrProcess3 = runtime.exec("/system/bin/ping -c 1 peepzcraft.zapto.org"); 
        int mExitValue3 = mIpAddrProcess3.waitFor(); 
        Process mIpAddrProcess4 = runtime.exec("/system/bin/ping -c 1 71.226.128.188"); 
        int mExitValue4 = mIpAddrProcess4.waitFor(); 
        Process mIpAddrProcess5 = runtime.exec("/system/bin/ping -c 1 playmcpe.com"); 
        int mExitValue5 = mIpAddrProcess5.waitFor(); 
        Process mIpAddrProcess6 = runtime.exec("/system/bin/ping -c 1 survival.dgpocket.us"); 
        int mExitValue6 = mIpAddrProcess6.waitFor(); 
        Process mIpAddrProcess7 = runtime.exec("/system/bin/ping -c 1 minecraft.blocksandgold.com"); 
        int mExitValue7 = mIpAddrProcess7.waitFor(); 
        Process mIpAddrProcess8 = runtime.exec("/system/bin/ping -c 1 pe.cookie-build.com"); 
        int mExitValue8 = mIpAddrProcess8.waitFor(); 
        Process mIpAddrProcess9 = runtime.exec("/system/bin/ping -c 1 96.8.119.195"); 
        int mExitValue9 = mIpAddrProcess9.waitFor(); 
        Process mIpAddrProcess10 = runtime.exec("/system/bin/ping -c 1 Play.mcpe-ba.info"); 
        int mExitValue10 = mIpAddrProcess10.waitFor(); 
        Process mIpAddrProcess12 = runtime.exec("/system/bin/ping -c 1 leet.cc"); 
        int mExitValue12 = mIpAddrProcess12.waitFor(); 
        Process mIpAddrProcess13 = runtime.exec("/system/bin/ping -c 1 ru.24serv.pro"); 
        int mExitValue13 = mIpAddrProcess13.waitFor(); 

        if(mExitValue==0){ 

         text.setText("Server Status: Online");    
        }else{ 
         text.setText("Server Status: Offline");    
        } 
        if(mExitValue2==0){ 
         text2.setText("Server Status: Online"); 
        }else{ 
         text2.setText("Server Status: Offline"); 
        } 
        if(mExitValue3==0){ 
         text3.setText("Server Status: Online"); 
        }else{ 
         text3.setText("Server Status: Offline"); 
        } 
        if(mExitValue4==0){ 
         text4.setText("Server Status: Online"); 
        }else{ 
         text4.setText("Server Status: Offline"); 
        } 
        if(mExitValue5==0){ 
         text5.setText("Server Status: Online"); 
        }else{ 
         text5.setText("Server Statis: Offline"); 
        } 
        if(mExitValue6==0){ 
         text6.setText("Server Status: Online"); 
        }else{ 
         text6.setText("Server Status: Offline"); 
        } 
        if(mExitValue7==0){ 
         text7.setText("Server Status: Online"); 
        }else{ 
         text7.setText("Server Status: Offline"); 
        } 
        if(mExitValue8==0){ 
         text8.setText("Server Status: Online"); 
        }else{ 
         text8.setText("Server Status: Offline"); 
        } 
        if(mExitValue9==0){ 
         text9.setText("Server Status: Online"); 
        }else{ 
         text9.setText("Server Status: Offline"); 
        } 
        if(mExitValue10==0){ 
         text10.setText("Server Status: Online"); 
        }else{ 
         text10.setText("Server Status: Offline"); 
        } 
        if(mExitValue12==0){ 
         text12.setText("Server Status: Online"); 
        }else{ 
         text12.setText("Server Status: Offline"); 
        } 
        if(mExitValue13==0){ 
         text13.setText("Server Status: Online"); 
        }else{ 
         text13.setText("Server Status: Offline"); 
        } 
       }catch(Exception e){ 

       } 
       return; 

답변

1

당신은 당신의 작업을 수행하는 안드로이드의 AsyncTask를 사용할 수 있습니다 여기에 내가 사용하고있는 핑 방법이다.

doInBackground()의 서버에 핑합니다.

결과를 onPostExecute (으)로 되 돌리십시오. &은 TextViews를 설정합니다.

백그라운드에서 현재 코드로 뷰를 업데이트 할 수 없습니다.

관련 문제