首先获取ps指令的打印信息:

  1. private static String getPsPrint(IDevice device) { 
  2.         OutputStream os = new ByteArrayOutputStream(); 
  3.  
  4.         try { 
  5.             device.executeShellCommand("ps"
  6.                     new OutputStreamShellOutputReceiver(os)); 
  7.             os.flush(); 
  8.             return os.toString(); 
  9.         } catch (TimeoutException e) { 
  10.             e.printStackTrace(); 
  11.         } catch (AdbCommandRejectedException e) { 
  12.             e.printStackTrace(); 
  13.         } catch (IOException e) { 
  14.             e.printStackTrace(); 
  15.         } catch (ShellCommandUnresponsiveException e) { 
  16.             e.printStackTrace(); 
  17.         } 
  18.  
  19.         return ""
  20.     } 

其中OutputStreamShellOutputReceiver类:

  1. public class OutputStreamShellOutputReceiver implements IShellOutputReceiver { 
  2.  
  3.     OutputStream os; 
  4.      
  5.     public OutputStreamShellOutputReceiver(OutputStream os) { 
  6.         this.os = os; 
  7.     } 
  8.      
  9.     public boolean isCancelled() { 
  10.         return false
  11.     } 
  12.      
  13.     public void flush() { 
  14.     } 
  15.      
  16.     public void addOutput(byte[] buf, int off, int len) { 
  17.         try { 
  18.             os.write(buf,off,len); 
  19.         } catch(IOException ex) { 
  20.             throw new RuntimeException(ex); 
  21.         } 
  22.     } 
  23.  

获得ps的打印信息之后,需要根据进程名分析出pid:

  1. public static String parsePid(IDevice device, String exeName) { 
  2.  
  3.         String content = getPsPrint(device); 
  4.         String[] pidInfos = content.split("\n"); 
  5.         for (String info : pidInfos) { 
  6.             if (info != null) { 
  7.                 // shell 18867 18856 852 300 c022c0bc afe0cdec S ./gsnap 
  8.                 if (info.contains(exeName)) { 
  9.                     String[] fragments = info.split(" "); 
  10.                     int idx = 0
  11.                     for (String str : fragments) { 
  12.                         if ((str != null) && (!str.trim().equals(""))) { 
  13.                             idx++; 
  14.                         } 
  15.  
  16.                         if (idx == 2) { 
  17.                             return str; 
  18.                         } 
  19.                     } 
  20.                 } 
  21.             } 
  22.         } 
  23.  
  24.         return ""
  25.     } 

最后,根据得到的pid,杀掉此android进程:

  1. public static void killProcess(IDevice device, String pid) { 
  2.         // 旧的手机,缺乏grep和awk程序! 
  3.         //String killCmd = "kill -9 `ps | grep \"gsnap\" | grep -v \"grep\" | awk '{print $2}'`"; 
  4.         String killCmd = "kill -9 " + pid; 
  5.                  
  6.         try { 
  7.             device.executeShellCommand(killCmd, 
  8.                     new OutputStreamShellOutputReceiver(System.out)); 
  9.         } catch (TimeoutException e) { 
  10.             e.printStackTrace(); 
  11.         } catch (AdbCommandRejectedException e) { 
  12.             e.printStackTrace(); 
  13.         } catch (IOException e) { 
  14.             e.printStackTrace(); 
  15.         } catch (ShellCommandUnresponsiveException e) { 
  16.             e.printStackTrace(); 
  17.         } 
  18.     } 

之所以会这么复杂,是因为这个:

  1. // 旧的手机,缺乏grep和awk程序! 
  2.         //String killCmd = "kill -9 `ps | grep \"gsnap\" | grep -v \"grep\" | awk '{print $2}'`";