2011-11-01 3 views
4

"ping"을 중지하는 방법은 무엇입니까? 다음 코드는 시도하는 방법이지만 실패합니다. "P == 널 stopProcess의"onPause에서 android에서 run.exec()가 시작한 프로세스를 중지하는 방법

private class pingWifi implements Runnable { 
      Process p = null; 
    @Override 
    public void run() { 
     // TODO Auto-generated method stub 
     /* 
     * WifiInfo info=mWifiManager.getConnectionInfo(); int 
     * ip=info.getIpAddress(); String ips=Formatter.formatIpAddress(ip); 
     */ 
     System.out.println("pingWifi runnable"); 
     String[] shell = new String[] { "/system/bin/sh", "-c", " " }; 
     Runtime run = Runtime.getRuntime(); 

     shell[2] = "ping " + ipAddress; 
     try { 
      p = run.exec(shell); 
      if (p.waitFor() != 0) { 
       if (p.exitValue() == 1) { 
        byte[] buff = new byte[1024]; 
        InputStream is = p.getErrorStream(); 
        int c; 
        c = is.read(buff, 0, 1024); 
        System.out.println("ping error: " + new String(buff)); 
       } 
      } else { 
       byte[] buff = new byte[1024]; 
       InputStream is = p.getInputStream(); 
       int c; 
       c = is.read(buff, 0, 1024); 
       System.out.println("ping result: " + new String(buff)); 
       // wait for hardware 
      } 
     } catch (Exception e) { 
      // TODO: handle exception 
      e.printStackTrace(); 
     } 

    } 
void stopProcess() { 
     if (p != null) { 
      p.destroy(); 
     }else{ 
      System.out.println("stopProcess: p==null"); 
     } 
    } 



    } 
private pingWifi mpingWifi = new pingWifi(); 
    Thread wifiThread = new Thread(mpingWifi); 
      wifiThread.start(); 

내가 mpingWifi.stopProcess()를 호출 한 결과이다. 그리고 "ps"목록에서 핑 프로세스를 찾을 수 있습니다.

"pkill ping"명령을 사용한다고 생각합니다. 하지만 "pkill"명령은 Android 셸에서 지원되지 않습니다. 내 목표는 "집 또는 등판"키 이벤트가 발생할 때까지 핑이 "중지"상태가 아니라는 것입니다. 감사!

답변

3

다시 버튼 누르기를 무시하고 그 안에 Process.destroy()를 호출 해보십시오.

예 : 답장을 보내 주셔서 감사 @kioboar

public void onBackPressed() { 
p.destroy(); 
} 
+0

. P는 무엇입니까? – Judy

+0

@ Judy p가 코드 'Process p = null'에 있습니다. – kikoboar

+0

하지만 "p는 null"입니다. "ping 192.168.11.3"이 프로세스는 항상 실행 중이며 해당 지점을 p로 반환하지 않습니다. 따라서 p는 null입니다. 맞아? – Judy

관련 문제